目录
目录
要指定你的项目所依赖的包,你必须在包的 package.json
文件中将它们列为 "dependencies"
或 "devDependencies"
。当你(或其他用户)运行 npm install
时,npm 将下载 package.json
中列出的满足每个列出的 语义版本 要求的 dependencies 和 devDependencies。要查看将安装哪个版本的包,请使用 semver 计算器。
¥To specify the packages your project depends on, you must list them as "dependencies"
or "devDependencies"
in your package's package.json
file. When you (or another user) run npm install
, npm will download dependencies and devDependencies that are listed in package.json
that meet the semantic version requirements listed for each. To see which versions of a package will be installed, use the semver calculator.
-
"dependencies"
:你的应用在生产中所需的包。¥
"dependencies"
: Packages required by your application in production. -
"devDependencies"
:仅用于本地开发和测试的包。¥
"devDependencies"
: Packages that are only needed for local development and testing.
将依赖添加到 package.json
文件
¥Adding dependencies to a package.json
file
你可以从命令行或通过手动编辑 package.json
文件将依赖添加到 package.json
文件。
¥You can add dependencies to a package.json
file from the command line or by manually editing the package.json
file.
从命令行向 package.json
文件添加依赖
¥Adding dependencies to a package.json
file from the command line
要从命令行向 package.json
文件添加依赖和 devDependencies,你可以使用依赖的 --save-prod
标志(也称为 -S
)(npm install
的默认行为)或 devDependencies 的 --save-dev
标志(也称为 -D
)将它们安装在包的根目录中。
¥To add dependencies and devDependencies to a package.json
file from the command line, you can install them in the root directory of your package using the --save-prod
flag (also -S
) for dependencies (the default behavior of npm install
) or the --save-dev
flag (also -D
) for devDependencies.
要将条目添加到 package.json
文件的 "dependencies"
属性,请在命令行上运行以下命令:
¥To add an entry to the "dependencies"
attribute of a package.json
file, on the command line, run the following command:
npm install <package-name> [--save-prod]
要将条目添加到 package.json
文件的 "devDependencies"
属性,请在命令行上运行以下命令:
¥To add an entry to the "devDependencies"
attribute of a package.json
file, on the command line, run the following command:
npm install <package-name> --save-dev
手动编辑 package.json
文件
¥Manually editing the package.json
file
要将 dependencies 添加到 package.json
文件,请在文本编辑器中添加一个名为 "dependencies"
的属性,该属性引用每个 dependency 的名称和 语义版本:
¥To add dependencies to a package.json
file, in a text editor, add an attribute called "dependencies"
that references the name and semantic version of each dependency:
{"name": "my_package","version": "1.0.0","dependencies": {"my_dep": "^1.0.0","another_dep": "~2.2.0"}}
要将 devDependencies 添加到 package.json
文件,请在文本编辑器中添加一个名为 "devDependencies"
的属性, 该属性引用每个 devDependency 的名称和 语义版本:
¥To add devDependencies to a package.json
file, in a text editor, add an attribute called "devDependencies"
that references the name and semantic version of each devDependency:
"name": "my_package","version": "1.0.0","dependencies": {"my_dep": "^1.0.0","another_dep": "~2.2.0"},"devDependencies" : {"my_test_framework": "^3.1.0","another_dev_dep": "1.0.0 - 1.2.0"}