Difference between npm install --save and npm inst

2019-02-25 09:32发布

问题:

Guys I know that using npm install -g we can install the node modules/packages globally, but I am not sure about the options --save and --save-dev

I have Googled it but still not clear about it. Please share your thoughts.

回答1:

--save adds the third-party package to the package's dependencies. It will be installed together with the package whenever someone runs nom install yourPackage.

--save-dev adds the third-party package to the package's development dependencies. It won't be installed when someone installs your package. It's typically only installed if someone clones your source repository and runs npm install in it.

Dev dependencies, as the same suggests, are those dependencies that are only needed for developing the package. That can include test runners, compilers, packagers, etc.

Both types of dependencies are stored in the package's package.json file. --save adds to dependencies, --save-dev adds to devDependencies. From the documentation:

devDependencies

If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.

In this case, it's best to map these additional items in a devDependencies object.

These things will be installed when doing npm link or npm install from the root of a package, and can be managed like any other npm configuration param. See npm-config(7) for more on the topic.

For build steps that are not platform-specific, such as compiling CoffeeScript or other languages to JavaScript, use the prepublish script to do this, and make the required package a devDependency.



回答2:

  • --save-dev is used to save the package for development purpose. Example: unit tests, minification.
  • --save is used to save the package required for the application to run.