I read about using the --save
option here and and it says that it will add the installed package to your package.json
file. But why isn't this automatic? Wouldn't you always want this?
My understanding is that the node_modules
is the directory that actually holds the code of your package and package.json
is the reference/list of all the packages you have installed so that when you push it up to a repo, you only push the latter up and not the former, to save space.
Then when other people clone or fork off your repo, they will have the package.json
to reference and install all the necessary packages to work off of your project.
Thus, wouldn't you always want your packages to be in the package.json
in order for everyone to get what is needed?
You don't need --save anymore for NPM installs. 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.
NPM provides the --save option while installing the packages. If we use the --save option after installing the package, it will be saved in package. json inside dependencies.
–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 desired results.
When we run the npm install package_name command, then the package name is added in the package. json file as a dependency. The --save specifies that the package is required at production or development.
With package managers like Bower or npm, I think --save
is not automatic for the following reasons:
--save-dev
).package.json
.Packages installed without --save
are not considered as dependencies and are kept separate. You can detect them easily as extraneous packages with npm ls
and remove them instantly with npm prune
.
Now if you think extraneous packages are a bad thing, you can of course use --save
everytime you install a new package. For practical reasons, be aware that you can use the -S
shortcut instead of --save
. Moreover, if you often forget to use the option, you could define an alias in your shell.
Finally, if you use Yarn, notice that the yarn add
command will add each package as a dependency. No --save
flag anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With