Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm: install and use different versions of a package

Tags:

Is there a way to install and also use the different package versions in npm? Installations works:

npm install -g [email protected]
npm install -g [email protected]
npm install -g [email protected]

I can install npm install -g [email protected] and check npm view web3 versions them. But how can I use them in the *.js script? Something like this:

require('[email protected]');
require('[email protected]');
require('[email protected]'); 
like image 755
baermathias Avatar asked Dec 18 '18 08:12

baermathias


2 Answers

There is a node module that allows you to do it.

npm-install-version

Install it: npm install npm-install-version --save-dev

var niv = require('npm-install-version');

niv.install('[email protected]');
niv.install('[email protected]');

var package_old = niv.require('[email protected]');
var package_new = niv.require('[email protected]');
like image 60
Sagar Rana Magar Avatar answered Sep 29 '22 02:09

Sagar Rana Magar


I did some research and it seems that its not possible with standard NPM. Here is the feature-request: https://github.com/npm/npm/issues/5499

However, with yarn (an NPM alternative from Facebook), you can do it out of the box by using yarn add and giving the package an alias.

I have not tried this for globally installed packages though, I assume it works the same.

like image 27
Lukas Knuth Avatar answered Sep 29 '22 00:09

Lukas Knuth