Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install multiple versions of the same node.js module?

Tags:

node.js

npm

Can I install multiple versions of the same node module globally with npm?

like image 556
Michael Avatar asked Aug 27 '12 08:08

Michael


2 Answers

I don't think there is a (good) way to do this.

However, I'm guessing that your use case is this: You have two projects, which require different versions of a globally installed package.

For cases like this, I usually avoid installing packages globally altogether, and install them locally instead (without -g). For example, if you wanted to install a specific older version of the "mocha" package for a given project, you'd do

cd ~/src/myproject
npm install --save-dev mocha@^1.0.0

(Note that we're not using -g here.) Then call it like so:

./node_modules/.bin/mocha
like image 110
Jo Liss Avatar answered Oct 04 '22 06:10

Jo Liss


While it is possible, it's probably going to be difficult to maintain without writing your own scripts to manage it. For this, I'm going to assume that you are using MacOS or Linux, and you have node installed in /usr/local/bin

When you install a global module, npm places the module and its dependencies in the same location that node is installed. Usually /usr/local/lib/node_modules, then creates a symbolic link for that module's in /usr/local/bin.

For example, you want to install nodemon, so you run npm install -g nodemon. npm installs nodemon to /usr/local/lib/node_modules/nodemon and creates a symlink at /usr/local/bin/nodemon that points to /usr/local/lib/node_modules/nodemon/bin/nodemon.js

Lets assume that you just installed [email protected], but for some reason you also need [email protected].

To have them both installed at once, but not conflict, you may be able to:

  1. rename the directory for your existing copy of nodemon from nodemon to nodemon18
  2. create a new nodemon18 symlink using ln -s /usr/local/bin/nodemon18 /usr/local/lib/node_modules/nodemon/bin/nodemon.js (don't forget to make it executable)
  3. install [email protected] via npm install -g [email protected]

Now, running nodemon from your terminal will run [email protected] and running nodemon18 will run [email protected].

YMMV. I really don't recommend doing this.

like image 22
mikefrey Avatar answered Oct 04 '22 05:10

mikefrey