If you ever installed an NPM package the following syntax looks very familiar to you:
npm install --save package_name
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
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",
}
}
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)
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?
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.
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.
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.
Author probably didn't know about this until recently.
I do know about it, but I saw a lot of tutorials still mention the --save command, so wanted to make this publicly know ❤️
Exactly! People still mention the old way to save dependencies even nowadays
Even the install can be shortened to "i".
npm i
npm i -D (for dev dependencies)
uninstall can me shortened to
uni
It can also be shortened to just
npm un
Woo this is awesome cuz i can never spell uninstall properly
or
npm rm
,npm r
may be usefulYep! ❤️
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
I think it will take some time, but we'll get there 🙏
use yarn instead
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?
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.I prefer yarn myself. Less verbose and more concise API in my opinion.
it caches packages, so it is fast
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.
yes, absolutely
At least give some reasons why!
its fast, easy and easy to remember commands
How is it different (better?) to yarn and npm?
can some one please explain me difference between --save and --save-dev
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.
Thanks for explanation. so just for clarification i will -> npm i bootstrap@latest --save
Yeah. But
--save
is also the default, so you can leave it out.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
I haven’t used Gatsby, so I can’t really compare the two.
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
Thanks for this great explanation Andrew! 🙏
Just like npm.
--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
orpip install -r requirements_dev.txt
have been doing
i -D
andi --save
. Never even tried a simplenpm i
. Never knew about that.I wish NPM can install production and development dependencies in one command, like this:
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.
The closest I can find is:
Detecting and installing Definitely Typed packages
I have literally never used --save flag. Pretty surprised to see this post.
This was needed before version 5 of NPM, so a lot of tutorials still have the save flag mentioned ❤️
Nice post!
In production build time, the optional packages how they will be handled?
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