how to install exact package lock version in package-lock.json

How to Install Exact Package Lock Version in package-lock.json?

If you have worked with Node.js projects, you may have come across the package-lock.json file. This file is generated automatically by npm to keep track of the exact versions of dependencies installed in your project. However, sometimes you may need to install a specific version of a dependency mentioned in this file. In this article, we will discuss how to install an exact package lock version in package-lock.json.

Option 1: Using npm install

The easiest way to install a specific version of a package is by using the npm install command. You can specify the package name and version number in the command as follows:

npm install package-name@version-number

For example, if you want to install version 1.2.3 of the lodash package, you can run the following command:

npm install lodash@1.2.3

This will update the package-lock.json file with the exact version number you have installed. If the package is not available in the specified version, npm will throw an error.

Option 2: Updating package-lock.json directly

If you want to update the package-lock.json file directly, you can do so by manually changing the version number of the dependency you want to update. You can open the file in a text editor and find the dependency section you want to update. Here's an example:


"dependencies": {
  "lodash": {
    "version": "1.2.3",
    "resolved": "https://registry.npmjs.org/lodash/-/lodash-1.2.3.tgz",
    "integrity": "sha1-9/1c7/2NjGW9+2/5algQFbKHmVc="
  }
}
  

You can update the version number in the "version" field and save the file. After that, you can run the npm install command to install the exact version you just updated.

Conclusion

These are the two options you have to install an exact package lock version in package-lock.json. The first option is easier and recommended, as it is less prone to errors. However, if you need more control over the package-lock.json file, you can use the second option.