要指定项目依赖的包,必须在你的包的 package.json 文件中将它们列为 "dependencies" 或 "devDependencies"。当你(或其他用户)运行 npm install 时,npm 会下载 package.json 中列出的满足各自 语义版本 要求的依赖和开发依赖。要查看将安装的包的版本,请使用 语义化版本计算器。
🌐 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":你的应用在生产中所需的包。"devDependencies":仅用于本地开发和测试的包。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 文件中,可以在你的包的根目录中使用 --save-prod 标志(也可以使用 -S)安装依赖(这是 npm install 的默认行为),或使用 --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
要向 package.json 文件添加依赖,在文本编辑器中添加一个名为 "dependencies" 的属性,该属性引用每个依赖的名称和 语义版本:
🌐 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"}}
要向 package.json 文件添加 devDependencies,在文本编辑器中,添加一个名为 "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"}