How to install and save exact npm package in Nodejs application

NodeJS has a dependency management using package.json and npm command

with the npm install command, It installs and adds a version as in the dependencies section

{
"package": "version"
}

The version contains a tild, carat symbols, or exact version.

During the npm install, if you don’t specify the version, if package.json contains 9.0.0

if the below is added to the version, It takes available latest version from the npm remote repository.

  • tild (~) : ~9.0.0 version allows allow minor versions such as 9.1.0, 9.2.0 9.3.0
  • carat(^) :^9.0.0 version allows allow patch versions such as 9.0.1, 9.0.2,9.0.3
  • exact version: mention as 9.0.0 to install the exact version

What does —save-exact do?

save-exact is used to install the npm package with the exact version. It helps developer to maintain stable versions for production builds.

npm —save-exact option

--save-exact option used to install the same version dependency to the node application

There are multiple ways we can set --save-exact option to the npm package.

npm install --save --save-exact @nestjs/core

It installs the exact version. These can be replaced with -E option which works a similar way

npm install --save -E @nestjs/core

Another way to set the npm configuration save-exact change to true.

npm config command sets save-exact config to true.

npm config set save-exact=true
  • Configure for project

In the Root project, Create or modify .npmrc file

save-exact=true
  • user level run the below command to add it to userdirectory/.npmrc folder
npm config set save-exact=true