Combining Node.js and Python Virtualenvs

Some notes on how to hack together a dual node.js and python venv

Whenever I have a new idea for a coding project the first thing I do is create a virtual environment (venv) so I can isolate all of the dependencies for the project and develop in a pristinely clean workspace.

I often develop with a mix of javascript and python so it would be really handy to setup virtualenvs that contain both isolated python and node installs. Well @evkalinin has the solution with nodeenv, a node.js virtual environment that can be baked right into python’s virtualenv.

Double Environment Setup

All you have to do is create your python venv, use pip to install nodeenv and then create a node venv within the python one. Here’s how:

$ mkvirtualenv my-node-python-project
[my-node-python-project] $ pip install nodeenv
[my-node-python-project] $ nodeenv --version
0.13.6
[my-node-python-project] $ nodeenv -p --prebuilt
[my-node-python-project] $ node -v
v5.7.1
[my-node-python-project] $ npm -v
3.6.0

I can also automate this process somewhat by adding the following to my python’s venv postmkvirtualenv hook so that nodeenv is always installed and the latest version of Node.js added whenever I create a new python venv:

pip install nodeenv
rehash
nodeenv -p --prebuilt

So now all I have to do to create my double python & node virtual environment is:

$ mkvirtualenv my-node-python-project

References

  1. ekalinin. Nodeenv. Node.Js Virtual Environments