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

What is the --save option for npm install?

1个答案

1

When using npm (Node Package Manager) to install dependencies, you can specify how dependency records are saved by adding parameters after the command. Here are some commonly used save option parameters:

  1. --save or -S: This parameter has been deprecated in npm 5+ because npm 5 defaults to saving dependencies to the dependencies section of the package.json file. Before npm 5, dependencies installed with --save are added to the dependencies section of package.json, indicating they are required for the project to run at runtime.

  2. --save-dev or -D: This parameter saves dependencies to the devDependencies section of the package.json file. Typically, these dependencies are only needed during development, such as for build tools and testing libraries, and are not used in production.

  3. --save-optional or -O: Dependencies installed with this parameter are added to the optionalDependencies section of package.json. These dependencies are optional for the project; even if they fail during installation, the overall process does not fail.

  4. --no-save: When installing dependencies with this option, npm will not modify the package.json or package-lock.json files. This is commonly used for temporarily installing dependencies without altering the current dependency state of the project.

  5. --save-exact or -E: This parameter installs a specific version of the dependency and records the exact version number in package.json instead of using version ranges.

  6. --save-peer: This parameter was not available in early versions of npm but was added in newer versions. It is used to explicitly mark dependencies as peer dependencies and add them to the peerDependencies object.

As an example, if you want to install a library named lodash and use it as a development dependency for the project, you can use the following command:

bash
npm install lodash --save-dev

This will add lodash to the devDependencies section of the project's package.json file. If you want to install a specific version of lodash and ensure every developer in the project uses the exact version, you can use:

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

你的答案