Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to Python's virtualenv in nodejs?

Consider Python right now:

  • You can have a single instance of the interpreter, say, 2.7.
  • You can install virtualenv, globally, and its wrappers (as long as you have permissions).
  • You can create several Python environments and install global packages you'd need like Django.
  • Once you activate the interpreter, you can run django-admin.py (a global command available in the environment) and create a django project.
  • Once you have your Django project, you can run python manage.py to run a command in your project.
  • Perhaps you can have pundle in your project to install packages that are project-specific and not environment-wide.

With this, you can have a project with its packages, inside of an environment with certain wide-scoped packages providing their own command line to be executed. You have a single interpreter installed here, and *for the same interpreter you can install several different Django versions (you will need a different environment for each version, but on each one the python interpreter is aliased in).

Now i would like to think about nodejs and something like react, cordova, sails, meteor... you have many options to pick one, as long as they provide a command line.

  • I install nodejs the first time, and also install npm and nvm.
  • I install a specific version of node with nvm. Perhaps I have to manually alias node to nodejs or viceversa due to many packages acting weird.
  • Instead of Django I'd like to create a Cordova/Ionic application with a specific Cordova version, so I npm install -g ionic in a specific nvm-picked version.
  • Equivalent to Pundle, npm has intrinsic support for my application.
  • If I want to sun a specific command for my cordova version for my project I can run one like npm run cordova -- build android instead of cordova build android.

But this brings me a problem: For the same node interpreter version I can have only one global cordova or ionic or whatever I want. This means: If I want to use a specific version of nodejs, and have two projects requiring different versions of the required command line, I cannot run a global command to create such projects. Example:

$ nvm use mynodeversion $ ionic start myApp

I will be using a specific version of ionic in that command, which will generate a that-version-specific boilerplate for my project. Assume the ionic version is x1.y1.z1.

Now I want to create a project with ionic version x2.y2.z2. But if I try to do this:

$ nvm use mynodeversion $ ionic start myApp

I will generate it for version x1.y1.z1 for the same interpreter, regardless which ionic version is referenced in my package.json.

In Python that is automatically solved with virtualenvs: you can have a specific Python interpreter, create many different environments with it, install different versions of your framework, one on each virtualenv, and generate different versions boilerplates for your projects, which will be compatible with the corresponding versions.

My question is: How can I do the same with nodejs? Provided, but not limiting to, the given example (another example could occur with sails or react-native).

like image 631
Luis Masuelli Avatar asked Jun 29 '16 16:06

Luis Masuelli


1 Answers

That's simple: never use npm install -g, use npm install --save-dev. CLI utilities will be installed in node_modules/.bin folder. For convenience it's also added to PATH for your npm scripts, so you can just call them without prefix.

like image 100
vkurchatkin Avatar answered Sep 30 '22 12:09

vkurchatkin