how to install multiple versions of package using npm

NpmPackage

Npm Problem Overview


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?

Npm Solutions


Solution 1 - Npm

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

Solution 2 - Npm

I wanted to post here for anyone like me that is using Yarn and landed here. It is a more or less drop-in replacement for NPM that supports aliasing out of the box:

yarn add material-ui@latest
yarn add material-ui-next@npm:material-ui@next
then

import FlatButton from 'material-ui/FlatButton'; // v0.x
import Button from 'material-ui-next/Button'; // v1.x

(credit for example goes to https://github.com/callemall/material-ui/issues/7195#issuecomment-314547601 )

Solution 3 - Npm

It sounds like "JSPM" might be exactly the tool you're looking for. JSPM builds on top of NPM but allows you to pull packages from multiple sources (github, npm, etc). It uses the System.js universal module loader on the front end for loading modules, and "uses flat version management to download into version-suffixed folders" that are easy to reason about.

jspm.io

When you install a package with jspm you can alias that package to a particular name, which you can later require specifically in your modules.

$ jspm install jquery
... (status msgs) ...
ok   Installed jquery as github:components/jquery@^2.1.4 (2.1.4)

$ jspm install jqueryOne=jquery@1.11.3
... (status msgs) ...
ok   Installed jqueryOne as github:components/jquery@1.11.3 (1.11.3)

      github:components/jquery 1.11.3 2.1.4

Then in your js, you can simply require(jquery) and/or require(jqueryOne) as necessary, allowing you to go back and forth as necessary.

This goes the same for any package which you'd like to use multiple versions of.

Solution 4 - Npm

In my case, I need to install an newer version 7 of react-table than the version I had installed i.e. react-table version 6 globally. So we were facing the problem for new development we need to use new version table without breaking old table functionality in application so I installed both the table with different key-

Ex.

  1. npm install react-table-7@npm:react-table@latest - new
  2. npm install react-table@npm:[email protected] - old

Solution 5 - Npm

This is quite difficult to do cleanly, due to the way npm works, so I would avoid attempting to do it in production.

However, for integration testing and similar use cases, I created a package called multidep, which lets you install multiple versions of the same package and require them like so:

var multidepPackages = require('multidep')('test/multidep.json');

var jquery1 = multidepRequire('jquery', '1.11.3');
var jquery2 = multidepRequire('jquery', '2.1.4');

Solution 6 - Npm

In my case, I needed to install an older version of create-react-app than the version I had installed globally, because I was taking a course that required this older version for the assignments.

I created a new folder just to contain this older version, cd'd into it, and did an

npm init

After setting up this shell package.json, I installed the exact version of create-react-app that I needed

npm install create-react-app@1.5.2

which created a local node_modules folder with the the older version of create-react-app.

Then I created a simple bash script (create-react-app.sh) as a shortcut to this older version, and used the bash variable "$@" to forward all arguments:

#!/bin/bash
{full-directory-path}/node_modules/create-react-app/index.js "$@"

Finally, I made this simple bash script executable

chmod u+x create-react-app.sh

So directly running this bash script will execute the older version of create-react-app:

./create-react-app.sh  --version
1.5.2

Solution 7 - Npm

I am not an expert in npm, but I had the following problem: two dependencies where competing for a version. I give the example, as for beginners it is hard to understand.

I have defined in package.json the following two dependencies:

"@ngtools/webpack": "^13.2.2",    
"angular-named-lazy-chunks-webpack-plugin": "^2.1.0",

they both demand a version of webpack, but a different one. They were competing. Your comments and solutions helped me, as I defined the following in devDependencies:

"webpack5": "npm:webpack@^5.68.0",
"webpack4": "npm:webpack@^4.46.0",

now, both "@ngtools/webpack" and "angular-named-lazy-chunks-webpack-plugin" are happy. The name webpack5 and webpack4 are random. I guess you can put whatever you like.

The error was:

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: openpaas-ssp@0.0.0
npm ERR! Found: webpack@4.46.0
npm ERR! node_modules/webpack
npm ERR!   dev webpack@"^4.46.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer webpack@"^5.30.0" from @ngtools/webpack@13.2.2
npm ERR! node_modules/@ngtools/webpack
npm ERR!   dev @ngtools/webpack@"^13.2.2" from the root project

When I was setting the 5.30 for webpack, then "@ngtools/webpack" was satisfied, but I was getting the similar error from "angular-named-lazy-chunks-webpack-plugin"

Solution 8 - Npm

NPM Install Version (https://github.com/scott113341/npm-install-version) is also an option. It essentially does what some of the other solutions here do (technically-speaking) but is quite straightforward to use. Modules installed with a version number (standard @version command param used by NPM) are predictably installed in a sub-folder under node_modules with that name. You can also control the destination dir per module - which is useful with build systems.

Usage code snippet from the GitHub Docs:

const niv = require('npm-install-version');
const benchmark = require('./some-benchmark-function.js');

niv.install('[email protected]');
// installs [email protected] to node_modules/[email protected]/

niv.install('[email protected]');
// installs [email protected] to node_modules/[email protected]/

const csjs_old = niv.require('[email protected]');
const csjs_new = niv.require('[email protected]');
// require the old and new versions of csjs

benchmark([csjs_old, csjs_new], 'some-test-input');
// run our fake benchmark function on the old and new versions of csjs

Solution 9 - Npm

install-npm-version (https://github.com/scott-lin/install-npm-version) is yet another option. It can be used on the command line, or through a programmatic interface -- written in TypeScript for modern development.

Example #1: Install to versioned (default) directory

import inv = require('install-npm-version');

inv.Install('[email protected]');
// installs chalk@2.4.0 to node_modules/chalk@2.4.0/

inv.Install('[email protected]');
// installs chalk@2.4.1 to node_modules/chalk@2.4.1/

Example #2: Install to custom directory

import inv = require('install-npm-version');

inv.Install('[email protected]', { 'Destination': 'some/path/chalk' });
// installs [email protected] to node_modules/some/path/chalk/

Example #3: Install with silent or noisy standard output

import inv = require('install-npm-version');

inv.Install('[email protected]', { 'Verbosity': 'Silent' });
inv.Install('[email protected]', { 'Verbosity': 'Debug' });

Example #4: Overwrite an existing installation

import inv = require('install-npm-version');

inv.Install('[email protected]', { 'Destination': 'mydir' });
// installs chalk@2.4.0 to node_modules/mydir/

inv.Install('[email protected]', { 'Destination': 'mydir' });
// does not install chalk@2.4.1 since node_modules/mydir/ already exists

inv.Install('[email protected]', { 'Destination': 'mydir', 'Overwrite': true });
// installs chalk@2.4.1 to node_modules/mydir/ by overwriting existing install

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarkView Question on Stackoverflow
Solution 1 - NpmRens BaardmanView Answer on Stackoverflow
Solution 2 - NpmtmahleView Answer on Stackoverflow
Solution 3 - NpmJemBijouxView Answer on Stackoverflow
Solution 4 - NpmMayuriView Answer on Stackoverflow
Solution 5 - NpmJo LissView Answer on Stackoverflow
Solution 6 - NpmStefan MusarraView Answer on Stackoverflow
Solution 7 - NpmAndreas PanagiotidisView Answer on Stackoverflow
Solution 8 - NpmSylonZeroView Answer on Stackoverflow
Solution 9 - NpmScott LinView Answer on Stackoverflow