Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there virtualenv for Node.js?

Tags:

node.js

I am totally new to Node.js. Can anyone tell me if there is anything similar to Python's virtualenv? I need to run apps on different versions of Node.js on the same machine.

like image 214
PaolaJ. Avatar asked Jan 20 '14 15:01

PaolaJ.


2 Answers

nvm by Tim Caswell

Check out nvm by Tim Caswell, it's a version manager for Node.js. It allows you to install multiple version of Node.js in parallel, and also manages version-independent variants of the global module cache for you. With nvm it basically comes down to:

$ nvm install 0.10.24
$ nvm use 0.10.24

And then you have Node.js 0.10.24 up and running, including the appropriate version of npm.

Installation of nvm is as easy as running a simple shell command:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

Another option may be n by TJ Holowaychuk or nave by Isaac Z. Schlueter, although I don't have any experience with either of them.

n by TJ Holowaychuk

Basically, they work quite similar to nvm. Installing n is as easy as running a shell script, too:

$ make install

Afterwards you are able to type:

$ n 0.10.24
$ n

Then you can select which installed version you'd like to use. From what I've heard n works better than nvm if you are on Windows.

nave by Isaac Z. Schlueter

nave is a shell script, again, and its usage is similar to that of nvm:

$ nave install 0.10.24
$ nave use 0.10.24

And that's it :-)!

like image 124
Golo Roden Avatar answered Sep 29 '22 08:09

Golo Roden


I remember that in Python - it comes down to using pythonbrew for switching between multiple versions of Python installed on the system (for example 2.7 and 3.0). For this - Node.js analogy would be in using nvm install as described by Golo.

When it comes to per project package management - in Python you would use virtualenv or virtualenvwrapper (even better) to organize modules and specific versions for your project. In Node.js this is assumed (that every project has own dependencies) - and you specify this in package.json file.

When you execute npm install all your packages are downloaded and installed in root_dir/.node_modules/ directory (which is local to project, assuming that root_dir is root of your Node project).

However, you can still install packages globally for node by using this command:

npm install -g "package_name"

General rule would be this:

  1. If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.

  2. If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

like image 37
azec-pdx Avatar answered Sep 29 '22 08:09

azec-pdx