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:
package.json 文件描述的程序的文件夹。<name>@<version>。<name>@<tag>。latest 标签的 <name>。git URL,当克隆时,会得到 (a)。🌐 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-ishgit+ssh://user@hostname:project.git#commit-ishgit+http://user@hostname/project/blah.git#commit-ishgit+https://user@hostname/project/blah.git#commit-ishcommit-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 子模块 或工作区。
🌐 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 文件的文件夹。要使用 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.