关于包和模块
See Details
目录
npm 注册表包含包,其中许多也是 Node 模块,或者包含 Node 模块。继续阅读以了解它们的不同之处以及它们如何相互作用。
¥The npm registry contains packages, many of which are also Node modules, or contain Node modules. Read on to understand how they differ and how they interact.
关于包
¥About packages
包是由 package.json 文件描述的文件或目录。包必须包含 package.json 文件才能发布到 npm 注册表。有关创建 package.json 文件的更多信息,请参阅“创建 package.json 文件”。
¥A package is a file or directory that is described by a package.json file. A package must contain a package.json file in order to be published to the npm registry. For more information on creating a package.json file, see "Creating a package.json file".
包可以不限定范围或限定给用户或组织,限定范围的包可以是私有的或公共的。有关详细信息,请参阅
¥Packages can be unscoped or scoped to a user or organization, and scoped packages can be private or public. For more information, see
-
"关于范围"
¥"About scopes"
-
"关于私有包"
关于包的格式
¥About package formats
包是以下任何一种:
¥A package is any of the following:
-
a) 包含由
package.json文件描述的程序的文件夹。¥a) A folder containing a program described by a
package.jsonfile. -
b) 包含 (a) 的压缩包。
¥b) A gzipped tarball containing (a).
-
c) 解析为 (b) 的 URL。
¥c) A URL that resolves to (b).
-
d) 与 (c) 一起在注册表上发布的
<name>@<version>。¥d) A
<name>@<version>that is published on the registry with (c). -
e) 指向 (d) 的
<name>@<tag>。¥e) A
<name>@<tag>that points to (d). -
f) 具有满足 (e) 的
latest标签的<name>。¥f) A
<name>that has alatesttag satisfying (e). -
g) 一个
giturl,在克隆时会导致 (a)。¥g) A
giturl that, when cloned, results in (a).
npm 包 git URL 格式
¥npm package git URL formats
用于 npm 包的 Git URL 可以通过以下方式格式化:
¥Git URLs used for npm packages can be formatted in the following ways:
-
git://github.com/user/project.git#commit-ish -
git+ssh://user@hostname:project.git#commit-ish -
git+http://user@hostname/project/blah.git#commit-ish -
git+https://user@hostname/project/blah.git#commit-ish
commit-ish 可以是可以作为参数提供给 git checkout 的任何标记、sha 或分支。默认 commit-ish 为 HEAD。
¥The commit-ish can be any tag, sha, or branch that can be supplied as an argument to git checkout. The default commit-ish is HEAD.
直接从 git 安装任何包都不会安装 git submodules 或工作区。
¥Installing any package directly from git will not install git submodules or workspaces.
关于模块
¥About modules
模块是 node_modules 目录中可以由 Node.js require() 函数加载的任何文件或目录。
¥A module is any file or directory in the node_modules directory that can be loaded by the Node.js require() function.
要由 Node.js require() 函数加载,模块必须是以下之一:
¥To be loaded by the Node.js require() function, a module must be one of the following:
-
包含
package.json文件的文件夹,其中包含"main"字段。¥A folder with a
package.jsonfile containing a"main"field. -
一个 JavaScript 文件。
¥A JavaScript file.
注意:由于模块不需要有 package.json 文件,因此并非所有模块都是包。只有具有 package.json 文件的模块也是包。
¥Note: Since modules are not required to have a package.json file, not all modules are packages. Only modules that have a package.json file are also packages.
在 Node 程序的上下文中,module 也是从文件加载的东西。例如,在以下程序中:
¥In the context of a Node program, the module is also the thing that was loaded from a file. For example, in the following program:
var req = require('request')
req 变量引用 require() 函数返回的 request 模块。
¥The req variable refers to the request module returned by the require() function.