Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to install multiple versions of package using npm

People also ask

Can you npm install multiple packages?

Installing multiple packages using package. When you run the npm install command without specifying any package name, then npm will look for an existing package. json file in the current working directory. npm will install all packages listed as dependencies of the project.

How do I install a different version of a package?

Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.

How do I use two npm versions?

If you need to use a different version of npm for each project, there are a number of possible solutions. Probably the lightest-weight version is to use npx . A semi-common use-case for this can be projects that use lock-file v1 and another that uses lock-file v2. v2 was introduced in npm v7.

How do I install two npm packages?

When we install a package using the npm install package-name command, it will download the current stable version of the package inside node_modules folder and add it to package. json file. To install multiple versions of the same package, we need to use the package alias syntax which is supported from the npm v6.


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

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 )


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.


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

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

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