16

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.

Nepoxx
  • 4,849
  • 5
  • 42
  • 61
  • 2
    If you are installing some debugging tools for instance, say `nodemon`, you'd not want it in your package.json – surajck Jan 07 '15 at 14:54

3 Answers3

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

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
  • 2
    Wouldn't it be easier/better to always use `--save` and run [depcheck](https://www.npmjs.com/package/depcheck) before comitting/deploying? – Nepoxx Jan 07 '15 at 14:53
  • well, as depcheck page on npm says, **But that's a hassle too.** – Naeem Shaikh Jan 07 '15 at 14:56
  • 1
    @Nepoxx what about when you are doing global installs, in that case adding it to the package.json would not make sense. – surajck Jan 07 '15 at 14:57
  • exellent @surajck.. shall I add your opinion to my answer. – Naeem Shaikh Jan 07 '15 at 14:59
  • @surajck But then you need the `-g` flag. – Nepoxx Jan 07 '15 at 15:01
  • @Nepoxx. But then It makes no sense, if you still use `--save` with `-g` – Naeem Shaikh Jan 07 '15 at 15:03
  • Yes, so you are also suggesting that, if there is a `-g` flag, then override the auto `--save` feature? – surajck Jan 07 '15 at 15:04
  • 3
    Your answer/comments explained very well why it is not so, but for the sake of argument, I **think** it would make sense to have `--save` by default (and therefore providing a `--nosave` option). So, `npm install something` would install something and save it, `npm install something -g` would install it globally (therefore not save it, since it would make no sense) and `npm install something --nosave` would install the dependency and not save it. In the end, it ends up being the same, I'm just not sure which one is more straightforward. – Nepoxx Jan 07 '15 at 15:12
  • Yes that would be really nice.. But we dont have it yet.. :) – Naeem Shaikh Jan 07 '15 at 15:22
  • 1
    I completely agree with @Nepoxx. This discussion deserves to go further in a github issue: https://github.com/npm/npm/issues/12031 – Augustin Riedinger Mar 23 '16 at 17:17
12

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.

Tod
  • 8,192
  • 5
  • 52
  • 93
2

By default with version npm 5.0+ npm install adds the module to the dependencies list in the package.json file; with earlier versions of npm, you must specify the --save option explicitly. Then, afterwards, running npm install in the app directory will automatically install modules in the dependencies list.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114