In Node.js, there are two popular ways to install a third-party library: NPM and Yarn. While both package managers enable us to install any libraries or modules present in the npm online registry, NPM is the most picked by developers.
With NPM, we usually install a package by running the npm install package-name command. But sometimes on the internet or in some documentation, we see a –save option and wonder what exactly it is and why it is used here whereas we do not use them usually when we install a package using NPM, right?
Well, here comes this tutorial where we will not only understand what this –save option for npm install means but also understand the usage of other types of save options like –save-dev, –save-prod, etc.
NPM Install –save Option
In this section, we will understand the purpose, usage and syntax of the –save option with the npm install command.
Purpose of –save for npm install
Before npm version 5.0.0, if we ran the command npm install, NPM installed the packages in the node_modules folder but did not update them as dependencies in the package.json file. So the –save or -S option is used to store the reference to the module in the package.json file’s dependency section. This –save option can also be abbreviated as -S.
Why List Dependencies in package.json?
Listing Dependencies in package.json is important when you actually host your application on the server. If the installed packages are present as dependencies in the package.json file, we can install them all into the server at once by running the npm install command, otherwise, it is hard to track how many packages we need to install manually, which version we installed during development etc.
Using –save or -S option
If you have the npm 5.0.0 or above version installed on your system, you may skip specifying the –save option, because with them when we use the npm command, the package automatically saves as dependencies in the package.json file by default. If you are using a lower version, the syntax is given below.
Syntax:
npm install <package_name> --save
Example:
npm install express --save
Output:
"dependencies": {
"express": "^4.18.2"
}
See the above code from package.json have express as dependencies.
Now that we are familiar with the –save option, we can discuss other types of save options.
Other NPM Save Options in Node.js
In this section, we will see other save options beyond the basic –save.
–save-dev or -D option
–save-dev or -D option installs a Node package as development dependencies or devDependencies. Dev dependencies are those 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 packages in the dependencies section and is the default option unless -D or -O is present. Any package under the dependencies 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 a package but do not want the package to be listed under any dependencies 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 of 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"
]
If you want to learn more about NPM or the Node Package Manager, we recommend reading our beginner guide to Node Package Manager.
Conclusion
In short, before npm version 5.0.0, the –save flag is used with npm install to easily install and save packages inside the dependencies section of your package.json file. After npm version 5.0.0, it becomes optional as NPM include it by default.
But it is still worth learning as it forms the basis of understanding other types of -save options such as –save-dev, –save-prod, etc which are still 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