DEV Community

Cover image for You don't need --save anymore for NPM installs
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

You don't need --save anymore for NPM installs

If you ever installed an NPM package the following syntax looks very familiar to you:

npm install --save package_name
Enter fullscreen mode Exit fullscreen mode

This was long the golden standard to install a package and save it as a dependency in your project.

Meaning if we didn't specify the --save flag, it would only get locally installed and not added to the package.json file.

NPM evolved

Over time NPM evolved into a huge player in package management, and ever since version 5 of NPM, we no longer need to define this --save argument.

Meaning our packages will be saved by default into our package.json file.

I'm thrilled with this addition, as it's very rare to want to install a package that you don't need in your package.json file.

Installing dev dependencies using NPM

We also used to have the following command to install a package as a dev dependency.

npm install --save-dev package_name
Enter fullscreen mode Exit fullscreen mode

This will place the package in your dev dependencies in the package.json file.

So to recap, the normal install will install our package under the dependencies, while the --save-dev argument will place them under devDependencies.

{
  "name": "my_project",
  "version": "0.0.1",
  "dependencies": {
    "package_name": "^1.0.0",
  },
  "devDependencies": {
    "package_dev_name": "^1.0.0",
  }
}
Enter fullscreen mode Exit fullscreen mode

NPM install additional flags

As we saw, the default install has no flags and will install our dependency. NPM, however gives us some flags to control the options.

  • -P, '--save-prod`: Package will install as a dependency
  • -D, --save-dev: Package will be installed as dev dependency
  • -O, --save-optional: Package will be installed as an optional dependency
  • --no-save: Package won't be saved in package.json file

These are the most important flags we can use. However, the only one you frequently use might be the -D flag.

Do keep in mind the letter flags are capital sensitive.

So to recap: we don't need to use the --save attribute anymore. This is now the default behavior.
We can provide the -D flag to save a package as a dev dependency.

Thank you for reading this article. I hope you learned something new today. And thank you, NPM for making this available.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (45)

Collapse
 
lionelrowe profile image
lionel-rowe

This has already been the case for 4 years, and npm has already had 2 more major versions since then. Handy info for people who didn't know, but why present it as if it's a new feature?

Collapse
 
david_woolf profile image
David Woolf

Some people aren’t aware of features, and it was a nice rundown of different commands. The author didn’t say it was a new feature, he specifically mentioned which NPM version came with the update.

Collapse
 
dailydevtips1 profile image
Chris Bongers

I don't present it as a new feature?
That's why I mentioned it's been available since NPM 5, but a lot of people still default to typing --save in their installs scripts, so I thought it would be a good idea to make this public announcement again.

Collapse
 
sanijalal profile image
Abd Sani

This is good info for me as there are tutorials out there in the wild that has the —save argument and I always wondered why it was needed. Now I know its no longer needed. Good to know.

Collapse
 
stojakovic99 profile image
Nikola Stojaković • Edited

Author probably didn't know about this until recently.

Collapse
 
dailydevtips1 profile image
Chris Bongers

I do know about it, but I saw a lot of tutorials still mention the --save command, so wanted to make this publicly know ❤️

Thread Thread
 
sami_hd profile image
Sami

Exactly! People still mention the old way to save dependencies even nowadays

Collapse
 
soumavabanerjee profile image
Soumava Banerjee

Even the install can be shortened to "i".

npm i
npm i -D (for dev dependencies)

Collapse
 
lukasdoesdev profile image
Lukas Does Dev

uninstall can me shortened to uni

Collapse
 
727021 profile image
Andrew Schimelpfening

It can also be shortened to just npm un

Thread Thread
 
toxiapo profile image
Leon

Woo this is awesome cuz i can never spell uninstall properly

Collapse
 
asazutaiga profile image
asazu taiga • Edited

or npm rm, npm r may be useful

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yep! ❤️

Collapse
 
milos192 profile image
Miloš Šikić

Thanks for this! I wanted to write the same post because I still see people using --save everywhere even though it's the default.

Now we just need to figure out how to raise the awareness further. :D

Collapse
 
dailydevtips1 profile image
Chris Bongers

I think it will take some time, but we'll get there 🙏

Collapse
 
mohdahmad1 profile image
Mohd Ahmad • Edited

use yarn instead

Collapse
 
727021 profile image
Andrew Schimelpfening

What reasons are there for using yarn instead, other than preference? Are there pros/cons of both? Yarn uses the NPM registry for packages, doesn’t it?

Collapse
 
benpai profile image
Benji Grant • Edited

Yarn has emojis, therefore it's better 💁‍♀️

On a more serious note though, it's mostly personal preference, and I really love that I can just run yarn and will install my packages, and it's output is a lot cleaner and easier to read than what npm produces.

Collapse
 
ptejada profile image
Pablo Tejada

I prefer yarn myself. Less verbose and more concise API in my opinion.

Collapse
 
mohdahmad1 profile image
Mohd Ahmad

it caches packages, so it is fast

Collapse
 
dailydevtips1 profile image
Chris Bongers

I think it's a personal preference, yarn did have some issues some years ago where their registry was down all the time, so decided to stick to NPM myself.

Thread Thread
 
mohdahmad1 profile image
Mohd Ahmad

yes, absolutely

Collapse
 
jezmck profile image
Jez McKean

At least give some reasons why!

Collapse
 
mohdahmad1 profile image
Mohd Ahmad

its fast, easy and easy to remember commands

Thread Thread
 
jezmck profile image
Jez McKean

How is it different (better?) to yarn and npm?

Collapse
 
pawandeore profile image
pawan deore

can some one please explain me difference between --save and --save-dev

Collapse
 
727021 profile image
Andrew Schimelpfening

Packages installed with —save-dev are only for development. In a production environment, they won’t be installed. You usually install things like linters, compilers, etc. as dev dependencies, since you don’t need them in production.

Collapse
 
pawandeore profile image
pawan deore

Thanks for explanation. so just for clarification i will -> npm i bootstrap@latest --save

Thread Thread
 
727021 profile image
Andrew Schimelpfening

Yeah. But --save is also the default, so you can leave it out.

Thread Thread
 
pawandeore profile image
pawan deore

Thanks. hey we are going to change our frontend stack and SEO is main factor so what do you recommend Gatsby or NExt js (the backend is going to be php based ZEND framework's API's) and you can consider our website has blogs where content writers write blogs daily (we have builtin platform for that) also video viewing subscription based platform just like udemy so this is all we got on website so what do you recommend

Thread Thread
 
727021 profile image
Andrew Schimelpfening

I haven’t used Gatsby, so I can’t really compare the two.

Thread Thread
 
killshot13 profile image
Michael R.

Pawan, you should check out StackShare, it is a great resource for these types of architecture/stack comparisons unlike SO which mostly features on use-case specific solutions. 👍

stackshare.io

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks for this great explanation Andrew! 🙏

Collapse
 
wintercounter profile image
Victor Vincent

Just like npm.

Collapse
 
waylonwalker profile image
Waylon Walker

--save-dev always confuses me, coming from python its generally the inverse. you add packages to your requirements.txt, then pip install -r requirements.txt or pip install -r requirements_dev.txt

Collapse
 
yw662 profile image
yw662

have been doing i -D and i --save. Never even tried a simple npm i. Never knew about that.

Collapse
 
yoursunny profile image
Junxiao Shi

I wish NPM can install production and development dependencies in one command, like this:

npm i -P react react-dom -D @types/react @types/react-dom
Enter fullscreen mode Exit fullscreen mode
Collapse
 
727021 profile image
Andrew Schimelpfening

That would be awesome. I wonder if that’s been suggested on the npm cli GitHub repo. It might be worth checking if there’s already an issue for it.

Collapse
 
yoursunny profile image
Junxiao Shi
Collapse
 
hasnaindev profile image
Muhammad Hasnain

I have literally never used --save flag. Pretty surprised to see this post.

Collapse
 
dailydevtips1 profile image
Chris Bongers

This was needed before version 5 of NPM, so a lot of tutorials still have the save flag mentioned ❤️

Collapse
 
majklzumberi profile image
majkl zumberi

Nice post!
In production build time, the optional packages how they will be handled?

Collapse
 
dailydevtips1 profile image
Chris Bongers

Not a 100% sure on that actually.
Never found a good use-case for optional packages.

But then again build-time will probably only evaluate the "need" packages