What is the –save option for npm install (with examples)

By default, all Node.js project uses NPM or Node Package Manager as their default package manager. We can use the command npm install or npm i followed by the package name and some options to install any dependency package from the node package manager. The– save option is one such option that the npm install or npm i command takes. In this article, we’ll discuss the –save option passed with npm install and also understand the use of other types of save options such as save-dev, save-prod, etc.

If you want to learn more about NPM or Node Package Manager, I’d highly suggest you read our Beginner’s Guide to Node Package Manager here.

–save or -S option

Before the advent of npm 5.0.0, the npm install command needed the –save option to install the mentioned package and store the reference to the module in the package.json file’s dependency section. This –save option can also be abbreviated as -S.

Dependencies are those Node packages without which your application won’t work in the production environment. If you have npm version 5.0.0 or above installed on your system, you may skip using the –save option as by default npm install or npm i followed by package name will install the package and store the reference in package.json.

Syntax:

npm install <package_name> --save

Example:

npm install express --save

Output:

"dependencies": {
    "express": "^4.18.2"
}

Now as we are familiar with the –save option, we can discuss other types of save options.

–save-dev or -D option

–save-dev or -D option installs the mentioned Node package as development dependencies or devDependencies. devDependencies are those dependency packages that are required for development and testing in the development environment. Such packages are not installed during production.

Syntax:

npm install <package_name> --save-dev

Example:

npm install nodemon --save-dev

Output:

"devDependencies": {
    "nodemon": "^2.0.21"
}

–save-prod or -P option

–save-prod or -P option performs the same operation as the –save option. It installs the package in the dependency section and is the default option unless -D or -O is present. Any package under the dependency section can be used both during production and development.

Syntax:

npm install <package_name> --save-prod

Example:

npm install express --save-prod

Output:

"dependencies": {
    "express": "^4.18.2"
}

–save-optional or -O option

–save-optional or -O option installs the Node package under the optionalDependencies section in package.json. Optional dependencies are those dependencies that don’t necessarily need to be installed. We may need optional dependency when we want npm to proceed even when a package fails to be installed or found.

Syntax:

npm install <package_name> --save-optional

Example:

npm install cheerio --save-optional

Output:

"optionalDependencies": {
    "cheerio": "^1.0.0-rc.12"
}

–no-save option

–no-save option is used when we want to install the package however we do not want the package to be listed under any dependency section.

Syntax:

npm install <package_name> --no-save

Example:

npm install express --no-save

Output:

No change in package.json

–save-exact or -E option

–save-exact or -E option installs the node package and configures the saved dependencies with an exact version instead npm’s default Semver range operator.

Syntax:

npm install <package_name> --save-exact

Example:

npm install express --save-exact

Output:

"dependencies": {
    "express": "4.18.2"
}

–save-bundle or -B option

–save-bundle or -B option installs the node package under the bundleDependencies section in package.json. bundleDependencies are the same as their name suggests. They are packed when we run the command npm pack. bundleDependencies are used when we want to reuse a third-party library not present in npm, use our own projects as a module, or want to distribute some files with our module.

Syntax:

npm install <package_name> --save-bundle

Example:

npm install express --save-bundle

Output:

"dependencies": {
    "express": "^4.18.2"
},
"bundleDependencies": [
    "express"
]

Conclusion

We need to understand that the –save option in npm install is now an optional flag however it’s always good to know about it as it’s the default option after npm version 5.0.0. Secondly, it forms the basis of understanding other types of save options such as save-dev, save-prod, etc which are useful when building scalable Node applications.

Wondering what to read next? Learn about the use of tilde(~) and caret (^) in package.json.

Reference

https://docs.npmjs.com/cli/v8/commands/npm-install

Devdeep Ghosh
Devdeep Ghosh
Articles: 14