Globally Install Two Different Versions of Node Package

February 6th 2020

Strapi.io is a great headless CMS option that we use for a lot of our clients websites. Although it is a headless CMS they have built a very nice admin dashboard that our clients can use to manage the content structure that we defined in our API for them. We have been fairly early adopters of the Strapi platform and have been using it since back in the alpha release phase. Less than a year ago Strapi.io went through a major release update from aplha to beta. They made some great changes to the platform, but not all of our clients were ready to update and go through a major release. However, we did need to continue working on the alpha version of the platform, all the while, servicing new clients who had the beta migration. For those of you who used the strapi@alpha version you remember that the installation required a global package install: npm install strapi@alpha -g which was all fine and dandy until you wanted to upgrade to beta. As you can see (and as expected) the beta version of the package retained the name. So if we were to run npm install strapi@beta -g it would essentially upgrade the global package to the beta version and we would not be able to work on platforms that only supported alpha!

What to do, what to do? Luckily we came up with a solution to have both package dependencies installed globally with one caveat…we had to give them a different alias name so when we ran the global strapi commands the package manager would know which package we’re referencing. Fortunately, recent versions of npm (version 6 and up) easily allow us to do this!

Our first step was to uninstall current version (strapi@alpha) so we could reinstall under a different alias: npm uninstall strapi@alpha -g. Okay, that was easy enough. Now for the reinstallation. Here is how you alias a package to a different name:

npm i <alias_name>@npm:<original_package_name>

In our case that looked like this:

npm i strapialpha@npm:strapi@alpha -g

At first glance that might look a little funny, but what it is saying is: “I want to install strapi@alpha globally but when I reference it I want the alias (name) to be strapialpha. You can confirm this by either running a global installation check: npm list -g --depth 0 or in the terminal by typing which strapialpha.

Now all we have to do is go about installing the beta version the in the same fashion that we’re used to (unless you want to call it something else like strapibeta), but in our case we just ran:

npm i strapi@beta -g

And there you have it. Two versions of the same package installed globally but with different aliases. Until next time, stay curious, stay creative!