Due to https://github.com/npm/npm/issues/2943, npm will never support the ability to alias packages and install multiple versions of the same package.
The workarounds posted on the github issue might work for pure-JS modules, but as npm becomes a standard for frontend package management, packages now include various assets such as CSS.
Is there any workaround to install multiple versions of the same package?
The best idea I've come up with is to "clone" a package, and publish it with a slightly different name.
For example, if you need multiple versions of jquery
, you could just publish packages called jquery-alias1
, jquery-alias2
, jquery-alias3
etc, and then set the appropriate versions in your package.json
.
Or you could name the packages according to their version number, eg jquery-1.11.x
, jquery-2.1.x
, etc..
Both of these approaches seem sloppy though. Are there better ones?
As of npm v6.9.0, npm now supports package aliases. It implements the same syntax as Yarn uses:
npm install jquery2@npm:jquery@2
npm install jquery3@npm:jquery@3
This adds the following to package.json
:
"dependencies": {
"jquery2": "npm:jquery@^2.2.4",
"jquery3": "npm:jquery@^3.4.1"
}
It is also possible to install directly from GitHub with this syntax. For example, if you want to install both the npm registry version and a GitHub fork of the package foobar
:
npm install foobar
npm install foobar-fork@github:username/foobar