Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wouldn't you want to use the `--save` option for npm install?

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?

like image 322
stackjlei Avatar asked Apr 30 '17 00:04

stackjlei


People also ask

Why do we not use -- save with npm install anymore?

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.

Do I need to use the -- save with npm install?

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.

What is the use of -- save in 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 desired results.

What is the difference between npm install and npm install -- save?

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.


1 Answers

With package managers like Bower or npm, I think --save is not automatic for the following reasons:

  1. Not all dependencies are production dependencies (see --save-dev).
  2. Sometimes you need to test a package without altering your package.json.
  3. You may prefer to install locally some packages that your colleagues installed globally on their computer.

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.

like image 115
Badacadabra Avatar answered Oct 16 '22 11:10

Badacadabra