2774

I saw some tutorial where the command was:

npm install --save

What does the --save option mean?

Dequog
  • 125
  • 12
Dmitri
  • 32,142
  • 8
  • 35
  • 54
  • 67
    @Tom- `--save: Package will appear in your dependencies.` – WiredPrairie Apr 12 '15 at 12:24
  • 14
    You can use shortcut -S and -D there -S is --save and -D is '--save-dev. --save (or simple -S): Package will appear in your dependencies. --save-dev (or simple -D): Package will appear in your devDependencies. – Gil Epshtain Feb 28 '16 at 20:47
  • 5
    @WiredPrairie thanks I was trying `npm install (--help | -h | -help ..)` and nothing. – juliangonzalez Aug 16 '16 at 20:14
  • 3
    thx @Dmitri, I had the same issue. come on NPM add that to your MAN page for your tool – Erdinc Ay Sep 13 '17 at 12:05
  • 150
    I don't understand the rationale behind the decision of removing even just a mention of this option from the docs and help page *without* also deprecating it, at the very least, with a warning in the tool itself. Meanwhile newbies are exposed to it via zillions of tutorials. Then they try to find out what it does and have to waste 20 minutes perusing the docs, to eventually end up reading npm history on stackoverflow. Oddly enough preventing this sort of massive waste of time (if the popularity of the question is any indication) is one of the purposes of documentation. – Michael Ekoka Jul 05 '18 at 05:30
  • @WiredPrairie Have u tried? U think such a 8k user ask a naive question? Please respect and be more careful. Thank you. – Nam G VU Aug 01 '18 at 19:46
  • 2
    @NamGVU - I thought the same thing at first, but it seems clear to me now that 5 years ago `--save` was included in the help command – Kyle Delaney Nov 21 '18 at 22:41
  • 3
    As of 2020 it does not exist as parameter anymore. See full answer in this thread for further info instead of the comments to the question. – Dirk Schumacher Jun 03 '20 at 19:25
  • 3
    @MichaelEkoka I am using React-Native for the first time and it is the finickiest piece of development software I have ever had. So far I've spent well over 20 hours debugging dependencies and zero time coding. It's great. There is no long term shared views between the modules. It's chaotic as hell and this is just another example it it. – FMaz008 Dec 20 '20 at 15:35

14 Answers14

3240

Update npm 5:

As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed. The other save options still exist and are listed in the documentation for npm install.

Original answer:

Before version 5, NPM simply installed a package under node_modules by default. When you were trying to install dependencies for your app/module, you would need to first install them, and then add them (along with the appropriate version number) to the dependencies section of your package.json.

The --save option instructed NPM to include the package inside of the dependencies section of your package.json automatically, thus saving you an additional step.

In addition, there are the complementary options --save-dev and --save-optional which save the package under devDependencies and optionalDependencies, respectively. This is useful when installing development-only packages, like grunt or your testing library.

voithos
  • 65,055
  • 11
  • 97
  • 112
  • 204
    Seriously, is this documented anywhere? I couldn't find it on Google or in the `npm` help. – Christian Ternus Oct 24 '13 at 23:56
  • 160
    It would be nice if documentation for this showed up when `npm install --help` was used. – Mark Stosberg Dec 06 '13 at 20:23
  • 2
    @MarkStosberg: It is listed under `npm help install`, which brings up the man page for `npm-install` on my machine. – voithos Dec 06 '13 at 23:43
  • 1
    I wanted to update all of my not-versioned (version = *) dependencies and set them to a fixed version. `npm install --save` does not update these in the package.json if I'm correct. If someone else wants to do what I did as well, see the next hideous command you can run: cat package.json | sed -E 's/"([a-z -_.]*)":[ ]*"\*",?/\1/g' | sed -E 's/[ ]*["|{|}].*//g' | tr '' ' ' | xargs npm install --save -frbl – frbl Sep 12 '15 at 12:31
  • 2
    Use `--save --save-exact` or `--save-dev --save-exact` to pin the semver range to a particular version, rather than using a caret. Also worth noting is that if you do e.g. `npm install sails@balderdashy/sails#master --save --save-exact`, you'll pin the dependency of the most recent commit on the master branch of the specified GitHub repo. – mikermcneil Oct 23 '16 at 18:27
  • -S, --save: Package will appear in your dependencies. – jawath Nov 09 '16 at 04:46
  • 1
    surprised this isn't the default, like composer – jim smith May 17 '17 at 10:39
  • 14
    @jimsmith: As of npm 5.0, it is the default. – voithos Jun 14 '17 at 00:14
  • 3
    Does "npm update" on NPM 5 modifies package.json by default? – Igor Okorokov Jul 03 '17 at 09:05
  • 1
    @IgorOkorokov: By default, I don't think so. `npm update` will comply with whatever version spec you already have in your package.json (so, it may perform an update, but only to a version that matches the specified pattern). If you want to save the updated version to your package.json, you can [pass `--save`](https://docs.npmjs.com/cli/update#recording-updates-with---save). – voithos Jul 03 '17 at 21:10
  • 7
    @voithos For me NPM 5 updates package versions in package.json. To avoid it i need to run 'npm update --no-save' – Igor Okorokov Jul 06 '17 at 14:57
  • I know I shouldn't downvote an answer because I think that NPM has made a dumb decision. But, it's so compelling. :) – Thomas Jan 10 '18 at 00:22
  • 3
    It appears that the obsolete save option has been removed from the official documentation of npm install. – David A. Gray Apr 09 '18 at 04:56
  • 1
    Just ran `npm install -g --save @google-cloud/vision`, it saved into `C:\Users\MyName\AppData\Roaming\npm\node_modules`, but it's not added into **package.json** at all. Any thought? – Jeb50 Jul 28 '18 at 23:20
  • so all these people adding --save to their npm commands these days are doing it out of habit. – nurettin Sep 18 '18 at 09:16
  • 2
    Now that --save is the default you can use --no-save to not automatically add the dependency to your package.json – Billkamm Aug 22 '19 at 15:20
  • @Jeb50 - *remove* `-g`, to add it to your project's package.json. See https://stackoverflow.com/a/8951576/199364, https://docs.npmjs.com/files/folders – ToolmakerSteve Oct 31 '19 at 13:00
  • Amazing that this is not documented anywhere but no matter. I noticed in the last couple of years that npm install and npm install --save did the same thing. – Harlin Aug 03 '20 at 15:30
  • It still makes a difference. E.g. installing react-router-dom fails without `--save`. – Soren Sep 08 '21 at 19:20
  • I have npm version 6. E.g, when I write npm install json-server, I already see it dependencies in package.json. In this case there is no difference between without save and with save. – A7x Mar 10 '22 at 13:09
314

Update as of npm 5:

As of npm 5.0.0 (released in May 2017), installed modules are added as a dependency by default, so the --save option is no longer needed.
The other save options still exist and are listed in the documentation for npm install.


Original Answer:

To add package in dependencies:

npm install my_dep --save

or

npm install my_dep -S

or

npm i my_dep -S

To add package in devDependencies

npm install my_test_framework --save-dev

or

npm install my_test_framework -D

or

npm i my_test_framework -D

package.json enter image description here

Henke
  • 2,831
  • 2
  • 16
  • 26
Joe L.
  • 3,929
  • 1
  • 17
  • 13
  • 42
    be careful using shortcut versions like `-S` `-D` as they must be uppercase. I always make this mistake and npm doesn't complain or add it to package.json – Murray Wynnes Aug 12 '16 at 14:23
  • 19
    It doesn't complain because `-s` (lowercase) is for the `--silent` option, and `-d` is for loglevel info which are both valid shortcuts. – tanvi Aug 21 '19 at 20:00
175

Update as of npm 5:

As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed. The other save options still exist and are listed in the documentation for npm install.


Original answer:

It won't do anything if you don't have a package.json file. Start by running npm init to create one. Then calls to npm install --save or npm install --save-dev or npm install --save-optional will update the package.json to list your dependencies.

temporary_user_name
  • 33,659
  • 44
  • 133
  • 199
Nick Retallack
  • 18,304
  • 17
  • 86
  • 111
  • 39
    Just found out that it works retroactively (it seems). You can run your `npm install --save-dev` first, then npm init and your package.json will be populated. – Jacob Wang Nov 15 '14 at 10:37
37

according to NPM Doc

enter image description here

So it seems that by running npm install package_name, the package dependency should be automatically added to package.json right?

ROROROOROROR
  • 871
  • 1
  • 13
  • 31
  • 2
    I guess --no-save is to use when you want functionality of like npm<5 – Abhiroj Panwar May 20 '18 at 08:01
  • I see, `npm config ls -l` shows that by default, save-xxx options are all false, only save is true. – ROROROOROROR May 21 '18 at 05:56
  • Looks like npm will not fire any error if you put a none exist option, like --save-xxxxxx. It just ignore it and the default behavior --save-prod will be in affect. Maybe --save comes from some old npm version. – Leon Mar 30 '20 at 06:54
18

You can also use -S, -D or -P which are equivalent of saving the package to an app dependency, a dev dependency or prod dependency. See more NPM shortcuts below:

-v: --version
-h, -?, --help, -H: --usage
-s, --silent: --loglevel silent
-q, --quiet: --loglevel warn
-d: --loglevel info
-dd, --verbose: --loglevel verbose
-ddd: --loglevel silly
-g: --global
-C: --prefix
-l: --long
-m: --message
-p, --porcelain: --parseable
-reg: --registry
-f: --force
-desc: --description
-S: --save
-P: --save-prod
-D: --save-dev
-O: --save-optional
-B: --save-bundle
-E: --save-exact
-y: --yes
-n: --yes false
ll and la commands: ls --long

This list of shortcuts can be obtained by running the following command:

$ npm help 7 config
Jim O'Brien
  • 2,472
  • 17
  • 29
DevWL
  • 14,674
  • 5
  • 82
  • 75
16

npm v6.x update

Now you can be using one of npm i or npm i -S or npm i -P to install and save module as a dependency.

npm i is the alias of npm install

  1. npm i is equal to npm install, means default save module as a dependency;
  2. npm i -S is equal to npm install --save (npm v5-)
  3. npm i -P is equal to npm install --save-prod (npm v5+)

check out your npm version

$ npm -v
6.14.4

get npm cli help info

$ npm -h

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    clean-install, clean-install-test, completion, config,
    create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
    edit, explore, fund, get, help, help-search, hook, i, init,
    install, install-ci-test, install-test, it, link, list, ln,
    login, logout, ls, org, outdated, owner, pack, ping, prefix,
    profile, prune, publish, rb, rebuild, repo, restart, root,
    run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami

npm <command> -h  quick help on <command>
npm -l            display full usage info
npm help <term>   search for help on <term>
npm help npm      involved overview

Specify configs in the ini-formatted file:
    /Users/xgqfrms-mbp/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@6.14.4 /Users/xgqfrms-mbp/.nvm/versions/node/v12.18.0/lib/node_modules/npm

get npm install help

npm help install alias npm -h i

$ npm help install
# OR, alias
# $ npm -h i

npm install (with no args, in package dir)
npm install [<@scope>/]<pkg>
npm install [<@scope>/]<pkg>@<tag>
npm install [<@scope>/]<pkg>@<version>
npm install [<@scope>/]<pkg>@<version range>
npm install <alias>@npm:<name>
npm install <folder>
npm install <tarball file>
npm install <tarball url>
npm install <git:// url>
npm install <github username>/<github project>

aliases: i, isntall, add
common options: [--save-prod|--save-dev|--save-optional] [--save-exact] [--no-save]
➜  ~ 

refs

https://docs.npmjs.com/cli/install

enter image description here

xgqfrms
  • 7,269
  • 1
  • 50
  • 53
11

As of npm 5, it is more favorable to use --save-prod (or -P) than --save but doing the same thing, as is stated in npm install. So far, --save still works if provided.

themefield
  • 3,120
  • 27
  • 32
  • 2
    This needs to go to the top to save us all an eyesore reading variations, contradictions and thread hijacks. SO needs to encourage succinct answers with valid URLs so we can avoid CRUD and get on with our work. (Quora has a neat 'Is this answer relevant' etc and I had even before that suggested to SO to do this) \n Sucks that the official node doc has no mention of older (defunct) options and most node references seem to be pre-5.0, sending our heads for a spin. – killjoy Apr 28 '18 at 13:10
10

npm install package_x --save

The given package (package_x) will be saved in package.json inside dependencies. if you add

npm install <<package_x>> --save-dev

then it will be saved inside devDependencies.

Lahiru Mirihagoda
  • 1,023
  • 1
  • 15
  • 29
Nizam Deen
  • 181
  • 1
  • 7
9

As of npm 5, npm will now save by default. In case,if you would like npm to work in a similar old fashion (no autosave) to how it was working in previous versions, you can update the config option to enable autosave as below.

npm config set save false

To get the current setting, you can execute the following command:

npm config get save

Source:https://blog.pusher.com/what-you-need-know-npm-5/

rajesh kumar
  • 1,270
  • 14
  • 13
7

–npm install --save or -S: When the following command is used with npm install this will save all your installed core packages into the dependency section in the package.json file. Core dependencies are those packages without which your application will not give the desired results. But as mentioned earlier, it is an unnecessary feature in the npm 5.0.0 version onwards.

npm install --save
Joundill
  • 5,418
  • 11
  • 31
  • 47
6

npm i (Package name) --save

Simplily, using above command we ll not need to write package name in your package.json file it ll auto add its name and dependency with version that you ll need at time when you go for production or setup another time.

npm help install

Above command ll help find out more option and correct def.shown in pic enter image description here

Nitin .
  • 754
  • 7
  • 11
6

npm install --save or npm install --save-dev why we choose 1 options between this two while installing package in our project.

things is clear from the above answers that npm install --save will add entry in the dependency field in pacakage.json file and other one in dev-dependency.

So question arises why we need entry of our installing module in pacakge.json file because whenever we check-in code in git or giving our code to some one we always give it or check it without node-modules because it is very large in size and also available at common place so to avoid this we do that.

so then how other person will get all the modules that is specifically or needed for that project so answers is from the package.json file that have the entry of all the required packages for running or developing that project.

so after getting the code we simply need to run the npm install command it will read the package.json file and install the necessary required packages.

Sunny Goel
  • 1,467
  • 13
  • 18
5

The easier (and more awesome) way to add dependencies to your package.json is to do so from the command line, flagging the npm install command with either --save or --save-dev, depending on how you'd like to use that dependency.

Rubin bhandari
  • 1,667
  • 13
  • 19
0

When you are using --save in the npm command to install a package , this means that your project will install that dependencies in the production enviroment, for example if you install a library to manage dates.

npm install moment --save 
npm i moment -S (same result)

(this is for production enviroment)

npm install moment --save--dev 
npm i moment -D (same result)

(this is for development enviroment)