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 package formats

包是以下任何一种:

🌐 A package is any of the following:

  • a) 一个包含由 package.json 文件描述的程序的文件夹。
  • b) 包含 (a) 的压缩包。
  • c) 解析为 (b) 的 URL。
  • d) 一个与 (c) 一起在注册表上发布的 <name>@<version>
  • e) 一个指向 (d) 的 <name>@<tag>
  • f) 一个具有满足(e)条件的 latest 标签的 <name>
  • g) 一个 git URL,当克隆时,会得到 (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-ishHEAD

🌐 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 子模块 或工作区。

🌐 Installing any package directly from git will not install git submodules or workspaces.

关于模块

🌐 About modules

模块node_modules 目录中任何可以通过 Node.js 的 require()import 语法加载的文件或目录。

🌐 A module is any file or directory in the node_modules directory that can be loaded by the Node.js require() or import syntax.

要被 Node.js 的 require() 函数加载,模块必须是以下之一:

🌐 To be loaded by the Node.js require() function, a module must be one of the following:

  • 一个包含 "main" 字段的 package.json 文件的文件夹。
  • 一个 JavaScript 文件。

要使用 import 语法,模块还应在其 package.json 文件中包含 "type": "module"

🌐 To use the import syntax, a module should also include "type": "module" in its package.json file:

{
"name": "my-package",
"type": "module"
}

**注意:**由于模块不要求必须有 package.json 文件,因此并非所有模块都是包。只有包含 package.json 文件的模块才是包。

在 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:

const req = require('request')

req 变量指的是由 require() 函数返回的 request 模块。

🌐 The req variable refers to the request module returned by the require() function.