Perspective Unspoken

My perspective on Git, Docker, Python, Django, PHP and other stuff

Using “virtual environments” with NodeJS

Python developers are quite spoilt with the good habit of using virtual environments for every project. What are they and why are they useful? Well, for starters, typically each project we develop uses other code from other libraries or frameworks. Every project has some dependencies, whether it be for database, HTML parsing, scraping, complex math, REST API libraries or some miscellaneous utilities. The list could go on. Everyone uses libraries. If you don’t… good luck reinventing the wheel.
Pinning down the versions of each of these libraries is essential to ensure there are no breakages because of an unplanned update in a library you were using. With Python, there typically is a requirements.txt file that lists all the libraries you use and their versions. In the Javascript world, there’s a package.json for that.

The next step though, is being able to have an environment where those only those libraries and their versions exist. Some libraries come with binaries as well, which means that only once you’re in that environment you have access to the binaries (at the specified version). Then, you can have several versions of these environments on your machine with different library versions.

In Python there is a way to “activate” the environment and naturally a way to deactivate it. This mechanism uses Linux’s source command. All of this functionality is provided by either the virtualenv or mkvirtualenv projects.

For NodeJS, there is a nodeenv project that gives the same functionality. It allows you to create a nice isolated environment in which you can globally install Node packages.


It can be installed using Pip.

To create an environment, just specify the location the environment should be created in.

This will install both Node and NPM in this environment. If you want to use the Node and NPM versions on the system, you can do that too!

This is a great option because for starters, NPM takes a little while to install and sometimes you want to do a quick test.

Once you’ve created your environment, you can source the environment.

This will allow you immediate access to all the binaries in the node_modules/bin  folder.


As a quick example, I was reviewing some of the changes in Typescript 2.0. I was able to bootstrap a sandbox very quickly using the following commands.





jaywhy13 • September 4, 2016

Previous Post

Next Post