How to install multiple versions of a npm package at the same time/project

3imed_jaberi

Imed Jaberi

Posted on July 25, 2021

How to install multiple versions of a npm package at the same time/project

Have you encountered when you are working on a project and you want to upgrade a dependency but you can’t do because you will have to migrate a lot of code or you should test your project with multi-versions of some module(s)?

Personally, I make and maintain a lot of Koa modules. Sometimes, I need to make the modules work with all versions of Koa so this is a real example how i do to solve this problem.

I found a solution by use a custom alias when installing a package with npm or yarn.

Alias allows you to install multiple versions of a same package in the same project.

You can use the alias by following this command:

with npm

npm i <your-alias>@npm:<package-name>
Enter fullscreen mode Exit fullscreen mode

with yarn

yarn add <your-alias>@npm:<package-name>
Enter fullscreen mode Exit fullscreen mode

When you want to install a specific version of the package append the command with @<package-version>.

Read the npm documentation here and/or yarn here to find more about alias.

For example, we want to use Koa with release 1.x.x and the latest one 2.x.x.

with npm

npm i koa-v1@npm:koa@1
npm i koa@npm:koa
Enter fullscreen mode Exit fullscreen mode

with yarn

yarn add koa-v1@npm:koa@1
yarn add koa@npm:koa
Enter fullscreen mode Exit fullscreen mode

Now, when you import the Koa module using koa-v1, it means that you are using koa@1.x.x. Otherwise, when importing with koa, it means that you are using the latest version of koa@ 2.x.x.

Did I miss something? Let me know in the comment section and let’s work on that.

Thank you for reading. I hope this will help you on your journey! ❤️

💖 💪 🙅 🚩
3imed_jaberi
Imed Jaberi

Posted on July 25, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related