mark
mark

Reputation: 5070

how to install multiple versions of package using npm

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?

Upvotes: 208

Views: 141065

Answers (12)

Edwin Hoogerbeets
Edwin Hoogerbeets

Reputation: 489

I know this is an old thread, but I after reviewing all of the above, I decided to write my own package that does what I want. I figured it might be useful to other people here. It's called conditional-install and it can install things based on various conditions. The conditions can be values of variables inside of process, such as process.versions.node or process.ENV.myvariable, or any of the nodejs features in node-compat-table.

How to Use it

Modify your package json to look like this:

{
    "name": "mypackage",
    "scripts": {
        "prepare": "conditional-install"
    },
    "devDependencies": {
        "conditional-install": "^1.0.1"
    },
    "conditionalDependencies": {
        "process.version >= 14.0.0": {
            "jest": "^29.0.0"
        },
        "process.version < 14.0.0": {
            "jest": "^26.0.0"
        }
    }
}

Under conditionalDependencies, you put properties that give the condition. Under each of those is a set of dependencies to install if that condition passes. In my example above, I install jest@26 when the version of nodejs that we're running on is less than v14.0.0, and jest@29 when the version is 14 or above.

To run the conditional-install when installing from npm or locally in your git clone, use the "postinstall" script instead, and put "conditional-install" in the dependencies instead of the devDependencies. The above example using the "prepare" script only runs locally in your git clone after a local npm install.

See the full README.md in the npm package for more details.

Upvotes: 0

fruitloaf
fruitloaf

Reputation: 2930

from FrontEnd developer.

I just tried it, and its horrible idea, because:

other packages that depend on your installed packages do NOT account for package name changes.

E.g.

Consider: Chart.js with React-Chartjs-2

Sure you can install:

"react-chartjs-2-old": "npm:react-chartjs-2@^2.11.2",
"chart.js-old": "npm:chart.js@^2.9.4",

but you immediately get:

  • Node package error modules - because react-chartjs-2 is importing chart.js as a dependency and NOT chart.js-old
  • Webpack: looking for chart.js, NOT chart.js-old

Yes! Yes! ... You can go into node.modules and manually change all the dependency requirements.

It will work for a cute 'todo' list app where YouTuber is telling you 'its magic'... but in an enterprise team, there are rebuilds happening all the time and node.modules get deleted and reset everyday, sometimes every hour.

So you will have to manually go into node_modules and change them every-time they get reset.

And pushing 20gigs of node_modules onto Production, not sure if its viable.

As a result your full time job will become 'managing node module dependencies' as oppose to doing any sort of real dev work.

Upvotes: 1

Dave Skender
Dave Skender

Reputation: 621

If trying to resolve conflicting peer dependencies after updating one package while another still relies on an old one, you can allow both to exist in different parts of your package tree:

npm install --legacy-peer-deps

Upvotes: 1

Mayuri
Mayuri

Reputation: 51

In my case, I need to install a 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

Upvotes: 5

Rens Baardman
Rens Baardman

Reputation: 5101

npm supports package aliases as of v6.9.0. It implements the same syntax as Yarn:

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. 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

Upvotes: 281

Andreas Panagiotidis
Andreas Panagiotidis

Reputation: 3032

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: [email protected]
npm ERR! Found: [email protected]
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/[email protected]
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"

Upvotes: 8

Stefan Musarra
Stefan Musarra

Reputation: 1511

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 [email protected]

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

Upvotes: 2

Scott Lin
Scott Lin

Reputation: 1562

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 [email protected] to node_modules/[email protected]/

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

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 [email protected] to node_modules/mydir/

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

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

Upvotes: 0

SylonZero
SylonZero

Reputation: 1207

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

Upvotes: 0

tmahle
tmahle

Reputation: 1217

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 )

Upvotes: 109

Jo Liss
Jo Liss

Reputation: 33124

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');

Upvotes: 3

JemBijoux
JemBijoux

Reputation: 104

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 [email protected]
... (status msgs) ...
ok   Installed jqueryOne as github:components/[email protected] (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.

Upvotes: 5

Related Questions