乐闻世界logo
搜索文章和话题

What is the --save option for npm install?

5 个月前提问
2 个月前修改
浏览次数133

6个答案

1
2
3
4
5
6

在使用npm(Node Package Manager)安装依赖包时,可以通过在命令后添加一些参数来指定安装时依赖记录的保存方式。以下是一些常用的保存选项参数:

  1. --save-S:此参数已在npm 5+中弃用,因为npm 5默认情况下会将依赖保存到package.json文件的dependencies部分。在npm 5之前,使用--save选项安装的依赖包会被添加到package.json中的dependencies部分,这意味着它们是项目运行时所必需的。

  2. --save-dev-D:此参数用于将依赖包保存到package.json文件的devDependencies部分。通常,这些依赖项只在开发过程中需要,如编译工具、测试库等,并不在生产环境中使用。

  3. --save-optional-O:使用此参数安装的依赖包会被添加到package.jsonoptionalDependencies部分。这些依赖是项目可以使用但不是必需的,即使在安装过程中它们失败了,整个安装过程也不会失败。

  4. --no-save:使用该选项安装依赖包时,npm将不会修改package.jsonpackage-lock.json文件。这通常用于临时安装依赖包,不希望更改项目当前的依赖状态。

  5. --save-exact-E:此参数用于安装特定版本的依赖包,并在package.json中记录确切的版本号,而不是使用版本范围。

  6. --save-peer:在早期版本的npm中不可用,但在较新版本中添加,这个参数是用来明确地将依赖包标记为peer依赖并将其添加到peerDependencies对象中。

作为一个例子,如果我想安装一个名为lodash的库并将其作为项目的开发依赖,我会使用以下命令:

bash
npm install lodash --save-dev

这将把lodash添加到项目的package.json文件中的devDependencies部分。如果我想安装特定版本的lodash并确保项目中的每个开发者都使用该确切版本,我可以使用:

bash
npm install lodash@4.17.15 --save-exact
2024年6月29日 12:07 回复

Update npm 5:

As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed. The other save options still exist and are listed in the documentation for npm install.

Original answer:

Before version 5, NPM simply installed a package under node_modules by default. When you were trying to install dependencies for your app/module, you would need to first install them, and then add them (along with the appropriate version number) to the dependencies section of your package.json.

The --save option instructed NPM to include the package inside of the dependencies section of your package.json automatically, thus saving you an additional step.

In addition, there are the complementary options --save-dev and --save-optional which save the package under devDependencies and optionalDependencies, respectively. This is useful when installing development-only packages, like grunt or your testing library.

2024年6月29日 12:07 回复

Update as of npm 5:

As of npm 5.0.0 (released in May 2017), installed modules are added as a dependency by default, so the --save option is no longer needed.
The other save options still exist and are listed in the documentation for npm install.


Original Answer:

To add package in dependencies:

shell
npm install my_dep --save

or

shell
npm install my_dep -S

or

shell
npm i my_dep -S

To add package in devDependencies

shell
npm install my_test_framework --save-dev

or

shell
npm install my_test_framework -D

or

shell
npm i my_test_framework -D

package.json

enter image description here

2024年6月29日 12:07 回复

Update as of npm 5:

As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed. The other save options still exist and are listed in the documentation for npm install.


Original answer:

It won't do anything if you don't have a package.json file. Start by running npm init to create one. Then calls to npm install --save or npm install --save-dev or npm install --save-optional will update the package.json to list your dependencies.

2024年6月29日 12:07 回复

According to NPM Doc:

Enter image description here

So it seems that by running npm install package_name, the package dependency should be automatically added to package.json, right?

2024年6月29日 12:07 回复

You can also use -S, -D or -P which are equivalent of saving the package to an application dependency, a development dependency or production dependency. See more NPM shortcuts below:

shell
-v: --version -h, -?, --help, -H: --usage -s, --silent: --loglevel silent -q, --quiet: --loglevel warn -d: --loglevel info -dd, --verbose: --loglevel verbose -ddd: --loglevel silly -g: --global -C: --prefix -l: --long -m: --message -p, --porcelain: --parseable -reg: --registry -f: --force -desc: --description -S: --save -P: --save-prod -D: --save-dev -O: --save-optional -B: --save-bundle -E: --save-exact -y: --yes -n: --yes false ll and la commands: ls --long

This list of shortcuts can be obtained by running the following command:

shell
npm help 7 config
2024年6月29日 12:07 回复

你的答案