Open In App

What is the –save option for npm install?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks and can be easily integrated into NodeJS projects to extend their functionality.

What is the –save option for npm install?

The –save option in npm install was used in older versions of npm to add a package to the dependencies section of the package.json file of your project.

When you install a package using npm install <package-name> --save, npm would download and install the specified package and also add it as a dependency in your package.json file.

For example, if you were installing a package named example-package, you would run:

npm install example-package --save

This command would install example-package into your project’s node_modules directory and also add an entry for example-package in the dependencies section of your package.json file, along with the specific version number installed.

However, starting from npm version 5.0.0, the –save option is no longer necessary. When you run npm install without any options, npm automatically saves the package to the dependencies section of package.json. So, you can simply run:

npm install example-package

And npm will install the package and update your package.json file accordingly.

The –save option is still supported by npm for backward compatibility, but it’s no longer required in modern versions of npm.

Example: Dependency before installing any npm package in react looks like:

"dependencies": {
        "react": "^18.2.0",
        "react-dom": "^18.2.0"
},

After installing a package i.e. react-router-dom using the code below:

npm i react-router-dom
// OR
npm i react-router-dom --save

The dependency list now has an addition, this includes react-router-dom along with it’s version i.e. 6.22.2 :

"dependencies": {
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-router-dom": "^6.22.2"
}

When we install a package using npm install package-name –save, it does the following:

  • Installs the Package: It downloads and installs the specified package from the npm registry.
  • Updates package.json: It adds the package to your project’s package.json file under the dependencies section. This means that the package is now considered a required dependency for your project.
  • Version Management: The –save option also specifies the version of the package you want to install. If you don’t specify a version, npm will install the latest stable version by default. However, if you want to pin your project to a specific version, you can include it like package-name@version
  • Dependency Resolution: When someone else clones your project or runs npm install in your project directory, npm will look at the dependencies section in package.json and install all the required packages listed there.

Conclusion:

In summary, although –save option is not needed to be mentioned explicitly after npm version 5.0.0, but using npm install package-name –save ensures that the package is installed, tracked as a dependency, and included in your project’s package.json file. This way, other developers working on your project can easily set up the same dependencies by running npm install.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads