如果你想依赖你自己模块中的包,你可以在本地 安装 一个包,使用 Node.js require 之类的东西。这是 npm install 的默认行为。

You can install a package locally if you want to depend on the package from your own module, using something like Node.js require. This is npm install's default behavior.

安装无范围的包

Installing an unscoped package

无范围的包始终是公开的,这意味着任何人都可以搜索、下载和安装它们。要安装公共包,请在命令行上运行

Unscoped packages are always public, which means they can be searched for, downloaded, and installed by anyone. To install a public package, on the command line, run

npm install <package_name>

这将在你的当前目录中创建 node_modules 目录(如果尚不存在)并将包下载到该目录。

This will create the node_modules directory in your current directory (if one doesn't exist yet) and will download the package to that directory.

注意:如果本地目录下没有 package.json 文件,则安装最新版本的包。

Note: If there is no package.json file in the local directory, the latest version of the package is installed.

如果有 package.json 文件,npm 会安装满足 package.json 中声明的 semver 规则 的最新版本。

If there is a package.json file, npm installs the latest version that satisfies the semver rule declared in package.json.

安装范围公共包

Installing a scoped public package

范围的公共包 任何人都可以下载安装,只要在安装时引用范围名称即可:

Scoped public packages can be downloaded and installed by anyone, as long as the scope name is referenced during installation:

npm install @scope/package-name

安装私有包

Installing a private package

私有包 只能由被授予对该包的读取权限的人下载和安装。由于私有包始终是有范围的,因此你必须在安装过程中引用范围名称:

Private packages can only be downloaded and installed by those who have been granted read access to the package. Since private packages are always scoped, you must reference the scope name during installation:

npm install @scope/private-package-name

测试包安装

Testing package installation

要确认 npm install 正常工作,请在你的模块目录中检查 node_modules 目录是否存在并且它是否包含你安装的包的目录:

To confirm that npm install worked correctly, in your module directory, check that a node_modules directory exists and that it contains a directory for the package(s) you installed:

ls node_modules

安装的包版本

Installed package version

如果运行 npm install 的目录中有 package.json 文件,npm 会安装满足 package.json 中声明的 语义版本控制规则 的最新版本的包。

If there is a package.json file in the directory in which npm install is run, npm installs the latest version of the package that satisfies the semantic versioning rule declared in package.json.

如果没有 package.json 文件,则安装最新版本的包。

If there is no package.json file, the latest version of the package is installed.

使用 dist-tags 安装包

Installing a package with dist-tags

npm publish 一样,npm install <package_name> 默认使用 latest 标签。

Like npm publish, npm install <package_name> will use the latest tag by default.

要覆盖此行为,请使用 npm install <package_name>@<tag>。例如,要在标有 beta 的版本上安装 example-package,你将运行以下命令:

To override this behavior, use npm install <package_name>@<tag>. For example, to install the example-package at the version tagged with beta, you would run the following command:

npm install example-package@beta

资源

Resources

npm 中文网 - 粤ICP备13048890号