Can I install multiple versions of the same node
module globally with npm
?
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
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:
nodemon
to nodemon18
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)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With