Question

How do I install a specific version of an npm package?

Answer and Explanation

To install a specific version of an npm package, you can use the npm install command followed by the package name and the desired version number. Here's how:

1. Open Your Terminal or Command Prompt:

- Navigate to your project's directory using the cd command.

2. Use the npm install Command with a Specific Version:

- The basic syntax is:

npm install <package-name>@<version-number>

- Replace <package-name> with the name of the npm package you want to install and <version-number> with the specific version you need.

3. Example:

- If you want to install version 16.8.0 of React, the command would be:

npm install react@16.8.0

4. Verify the Installation:

- After the installation is complete, you can verify the installed version by checking your package.json file in the project directory. The dependencies section should list the package with the specified version.

- Alternatively, you can use the command:

npm list <package-name>

- This will display the installed version of the specified package.

5. Saving the Exact Version:

- By default, npm uses semantic versioning (semver) ranges. If you want to ensure you install and save the exact version specified without allowing minor or patch updates, you can use the --save-exact flag (or set save-exact=true in your npm configuration):

npm install --save-exact react@16.8.0

By following these steps, you can install a specific version of any npm package in your project. This is useful when you need to maintain compatibility with specific versions or avoid breaking changes introduced in later versions.

More questions