What is the meaning of --save in npm install?

--save is a command-line option used with the npm install command. It installs a package locally. Local installation of a package means all necessary files and dependencies of that package are downloaded and installed. It also saves the package name and its version in the dependencies section of the package.json file.

-S is the alias of --save, which means both --save and -S are the same. So you can use either -S or --save.

Packages that are installed using the --save option become the core dependencies of the project.

This is how you use the --save option with the npm install command:

$npm install [package-name] --save

For example, to install the socket.io package locally, run the following command:

$npm install socket.io --save

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

Before NPM version 5.0.0, you were required to specify the --save option for installing the package locally and updating the dependencies section of the package.json file. However, in the latest version of NPM, you are not required to mention the --save option. This is done to save you from unnecessary typing.

$npm install [package-name]
$npm install [package-name] --save

Now both npm install [package-name] and npm install [package-name] --save are same. These two commands install the package locally and add that package to the dependencies list in package.json. You can use either of them.

Let's see how --save option works with the npm install command.

Suppose you want to install the express package. The content of package.json before running npm install command.

{
  "name": "notes-app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Mohit Natani",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

In this example, you can see lodash package is a core dependency of the project

Run npm install command to install the express package.

$npm install express --save
OR 
$npm install express

The package.json file after running npm install command.

{
  "name": "notes-app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Mohit Natani",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1",
    "lodash": "^4.17.21"
  }
}

You can see that express and its version is mentioned in the dependencies section of the package.json file.

Recommended Posts