Usually, you install the latest version of a package when you want to use it. But there are circumstances when you might need to install a specific package version.
One example would be when the latest changes in a package are not compatible with your code.
This article shows you how to install a specific package version to avoid such situations. You will learn to do it using both
With npm, you install a package as follows:
npm install package_name
npm i package_name
npm i is the shortcut for
The above commands install the latest version of the specified package.
But what if you want to install a specific version? The syntax for installing a particular package version is as follows:
npm install package_name@version_number
When running the above command, npm installs the version specified after the "@" symbol.
If you want to see all the versions of a package, you can do it with the following command:
npm view package_name versions
That will list all the versions from the first to the last one.
With yarn, you can accomplish the same thing as follows:
yarn add package_name@version
The idea is the same, but the syntax differs.