I understand the differences between npm install something
and npm install something --save
(for anyone wondering, the first one will install the dependency only while the latter will install the dependency and add it to your package.json).
However I do not understand why there is a --save
option in the first place. In other words, why would you ever want to install a dependency without adding it to your package.json file? Why is the --save option not default?
A lot of websites/npm modules/SaaS suggest installing their module using npm install something
(newrelic is one of them for instance), am I missing something?
Edit: Starting from NPM 5, --save
is now on by default.
You would have a scenario such as you need some module to install without adding dependency to
package.json
file, for ex. you just want to try some module, and not sure you would be really using that module in production or while deploying, so instead adding the module dependency topackage.json
, just give it a try without using--save
. this is whynpm install
without--save
exists.But For most of your modules you might require using
--save
, for ex.npm install express --save
, in this case you surely know that you are going to use express for you application.The other scenario, for not using
--save
, would be,npm install heapdump
ornpm install nodemon
, I would use it for testing my apps performance, but not add a dependency in the package.json :)Also, As @surajck said in comment below: when you are doing global installs, in that case adding dependencies using
--save
, to thepackage.json
would not make sense.I just learned a nice trick from Jonathan Mills' JavaScript Best Practices course on Pluralsight. From the terminal:
npm config set save=true
Now I don't need to remember
--save
anymore. And I also now usenpm config set save-exact=true
Because I want the exact version of the package not the
^
prefix.