Benefits of Using the `–save` Flag in npm Install

One possible solution is to use package managers such as Bower or npm, which may not provide automatic updates. This is because not all dependencies are production dependencies, as discussed in the previous section. Additionally, it’s worth noting that the user’s node version is 10.24.1 and their npm version is 6.4.1, and they utilize nvm for Windows to manage versions.


Question:

Upon reading about the

--save

option, I discovered that it involves adding the installed package to the

package.json

file. However, I wonder why this isn’t a default setting since it seems like a beneficial feature.

As I comprehend it, the directory referred to as

node_modules

contains the actual code for your package, while

package.json

serves as a list of all the installed packages. This allows you to push only the latter to a repository to save space.

Once your repository is cloned or forked by others, they can refer to

package.json

to install all the required packages and start working on your project.

Therefore, wouldn’t it be preferable for all to receive what is required if your packages are consistently within the

package.json

?


Solution 1:

Considering the availability of package managers such as Bower or npm, I believe that the process of

--save

cannot be considered automatic due to the following factors:

  1. Some dependencies, as shown in

    --save-dev

    , are not meant for production use.
  2. At times, it may be necessary to perform a package testing without making any changes to the

    package.json

    .
  3. You might choose to install certain packages on your computer that your coworkers have already added.

If a package is installed without

--save

, it will not be classified as a dependency and will remain isolated. These extraneous packages can be identified with

npm ls

and removed immediately using

npm prune

.

If you view unnecessary packages as undesirable, you can utilize

--save

during each installation. It is important to note that

-S

can be substituted for

--save

for convenience. Additionally, if you frequently forget to apply this option, consider creating a shell alias.

When using Yarn, it’s important to note that the

yarn add

command automatically includes each package as a dependency, eliminating the need for the

--save

flag.


Solution 2:


According to one of the maintainers of npm:

Over the past few years, significant changes have occurred that make certain aspects of this problem irrelevant. Despite this, executing

npm config set save true

as a regular user is a straightforward task. However, there are still several challenges to overcome when setting

--save

as the default option.

  • Although it can be frustrating to have to recall either

    --save

    or

    --save-dev

    during installation, it does present the benefit of requiring you to designate whether the package is a

    dependency

    or a

    devDependency

    at the time of installation, which can help alleviate cognitive strain.
  • Transferring packages within

    package.json

    can be challenging, especially if you forget to label something as a devDependency, resulting in difficulty in cleaning up. Therefore, it may not be advantageous to enable automatic saving for everyone as a default option.

The source of the following statement is a GitHub issue on the npm repository with the issue number 5108.

Frequently Asked Questions