3093

I saw some tutorial where the command was:

npm install --save

What does the --save option mean?

10
  • 21
    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. Commented Feb 28, 2016 at 20:47
  • 6
    @WiredPrairie thanks I was trying npm install (--help | -h | -help ..) and nothing. Commented Aug 16, 2016 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
    Commented Sep 13, 2017 at 12:05
  • 205
    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. Commented Jul 5, 2018 at 5:30
  • 7
    @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
    Commented Dec 20, 2020 at 15:35

16 Answers 16

3570

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.

20
  • 234
    Seriously, is this documented anywhere? I couldn't find it on Google or in the npm help. Commented Oct 24, 2013 at 23:56
  • 171
    It would be nice if documentation for this showed up when npm install --help was used. Commented Dec 6, 2013 at 20:23
  • 18
    @jimsmith: As of npm 5.0, it is the default.
    – voithos
    Commented Jun 14, 2017 at 0:14
  • 3
    Does "npm update" on NPM 5 modifies package.json by default? Commented Jul 3, 2017 at 9:05
  • 8
    @voithos For me NPM 5 updates package versions in package.json. To avoid it i need to run 'npm update --no-save' Commented Jul 6, 2017 at 14:57
372

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

3
  • 51
    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 Commented Aug 12, 2016 at 14:23
  • 31
    It doesn't complain because -s (lowercase) is for the --silent option, and -d is for loglevel info which are both valid shortcuts.
    – tanvi
    Commented Aug 21, 2019 at 20:00
  • great explaining Commented May 1 at 13:11
182

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.

1
  • 43
    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
    Commented Nov 15, 2014 at 10:37
47

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?

4
  • 2
    I guess --no-save is to use when you want functionality of like npm<5 Commented May 20, 2018 at 8:01
  • I see, npm config ls -l shows that by default, save-xxx options are all false, only save is true. Commented May 21, 2018 at 5: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
    Commented Mar 30, 2020 at 6:54
  • Please review Why not upload images of code/errors when asking a question? (e.g., "Images should only be used to illustrate problems that can't be made clear in any other way, such as to provide screenshots of a user interface.") and do the right thing (it covers answers as well). Thanks in advance. Commented Jan 27, 2023 at 3:09
22

You can also use -S, -D or -P which are equivalent of saving the package to an application dependency, a development dependency or production 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
0
21

npm v6.x update

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

npm i is the alias of npm install

  1. npm i is equal to npm install, meaning the 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 information

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

[email protected] /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

Output:

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]
➜  ~

References

npm-install

Enter image description here

15

Use:

npm install package_x --save

The given package (package_x) will be saved in file package.json inside dependencies.

If you add

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

then it will be saved inside devDependencies.

2
  • 19
    What does your answer contribute that other answers haven't covered already?
    – Pedro A
    Commented Jul 28, 2017 at 0:49
  • 6
    @Pedro A -- My suspicion is that the guy rewrites what he just read; then has his friends give him up-votes.
    – IAM_AL_X
    Commented Feb 13, 2020 at 22:29
12

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.

1
  • 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
    Commented Apr 28, 2018 at 13:10
10

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: Here’s what you need to know about npm 5

10

–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
7

npm install --save or npm install --save-dev is why we choose one option between these two, while installing the package in our project.

Things are clear from the previous answers that npm install --save will add an entry in the dependency field in the package.json file and other one in dev-dependency.

So the question arises: Why do we need an entry of our installing module in package.json file, because whenever we check in code in Git or giving our code to someone, we always give it or check it without the node_modules folder, because it is very large in size and is also available at a common place, so to avoid this, we do that.

So then how another 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.

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.

5

npm i (Package name) --save

Simply, using the above command we’ll not need to write the 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 set up another time.

npm help install

The above command will help to find out more options and correct def.shown in the picture:

Enter image description here

2
5

In npm version 4, if you run the command npm install express without the --save or --save-dev flags, npm will still install the express package, but it will not add it to the dependencies or devDependencies section of your package.json file.

To explicitly save the dependency in the package.json file, you should use the --save flag:

npm install express --save

If you don't use the --save flag, the package will be installed locally in the node_modules directory, but the information about the dependency won't be added to the package.json file.

Starting from npm version 5 and later, npm introduced the automatic saving of dependencies without the need for the --save flag. If you are using a version of npm that is 5 or later, running npm install express will automatically add the dependency to the dependencies section of your package.json file.

0

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

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

(this is for a production environment)

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

(this is for a development environment)

0
npm i pdf.js-viewer --save

In npm, the --save flag is used to automatically update the dependencies section of your package.json file with the installed package and its version. It indicates that the package should be added as a dependency of your project. This is particularly useful when you want to keep track of the dependencies used in your project and ensure that others working on the project can easily install the same dependencies.

For example, when you run:

npm i pdf.js-viewer --save

It will install the pdf.js-viewer package and add an entry to your package.json file under the dependencies section, like this:

"dependencies": {
  "pdf.js-viewer": "^x.x.x"
}

The ^x.x.x signifies that npm can install any future compatible version of the package when you run npm install in the future.

If you don't include the --save flag when installing a package, npm will still install the package locally, but it won't add it to your package.json file. This means that you won't have an explicit record of the package as a dependency of your project, and others working on the project won't automatically install it when they clone your project or run npm install.

If you're not working in an Angular project, the installation process remains the same. However, the usage and integration of the pdf.js-viewer package may vary depending on the framework or environment you're working with. Make sure to consult the package's documentation for instructions specific to your project setup.

Not the answer you're looking for? Browse other questions tagged or ask your own question.