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>
with yarn
yarn add <your-alias>@npm:<package-name>
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
with yarn
yarn add koa-v1@npm:koa@1
yarn add koa@npm:koa
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
.
Top comments (2)
Thank you for the article. It helped me a lot in the current project
Very cool trick indeed! It helped me a lot today as I needed features from a beat API while still wanting to use the stable legacy API for most of the other parts. 👍