如果你想依赖你自己模块中的包,可以在本地对包进行 安装,使用类似 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 文件,将安装该软件包的最新版本。

如果存在 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

任何人都可以下载并安装 带作用域的公共包,只要在安装过程中引用了作用域名称即可:

npm install @scope/package-name

安装私有包

🌐 Installing a private package

私有软件包 只能由被授予包读取权限的人下载和安装。由于私有包始终有作用域,因此在安装时必须引用作用域名称:

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,你可以运行以下命令:

npm install example-package@beta

资源

🌐 Resources