npm install --save, what is the use of not saving

2019-03-24 19:19发布

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.

标签: node.js npm
2条回答
Root(大扎)
2楼-- · 2019-03-24 19:44
  1. 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 to package.json, just give it a try without using --save. this is why npm install without --save exists.

  2. 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.

  3. The other scenario, for not using --save, would be, npm install heapdump or npm install nodemon, I would use it for testing my apps performance, but not add a dependency in the package.json :)

  4. Also, As @surajck said in comment below: when you are doing global installs, in that case adding dependencies using --save, to the package.json would not make sense.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-24 19:59

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 use
npm config set save-exact=true
Because I want the exact version of the package not the ^ prefix.

查看更多
登录 后发表回答