Creating a nodejs/javascript virtual environment using pipenv and nodeenv
Creating a node virtual environment
So as I delve into different react tutorials I will encounter multiple versions of node, which means that to follow along with each tutorial I should make sure my local environment (including node) matches that used in the tutorial to avoid errors caused by incompatible versions. Hmm.. this means uninstalling the version of node that I have installed and installing the one specified in the tutorial. What if I want to use different versions of node at the same time to work on two different projects or to follow along with different tutorials that each use different node versions, what to do then?
A solution that works for me is using nodeenv which is a tool to create nodejs virtual environments, this enables me to work on different projects each using different versions of node.
First step: Create a virtual environment using pipenv
Pipenv makes it super easy to manage virtual environments the process is as follows
mkdir projectFolder && cd projectFolder
pipenv shell
pipenv install nodeenv
With these commands, you have sucessfuly created a virtual environment and downloaded nodeenv, time to install an isolated instance of nodejs in the environment
Second Step: Install a specific version of nodeenv
You can install node in a subdirectory and then activate it using the command
nodeenv nodefolder
acctivate the node folder to use node
nodefolder/bin/activate
check the node version using the command
node -v
By default not including the node version will install the latest version so the command node -v
will indicate the latest version of node
Instead of creating a folder in which to install node, you can also bind the node environment and the pre existing environment created by pipenv using the command
nodeenv -p
to deactivate the node environment use deactivate_node
Install a specific version of node
In this specific instance I want to install node version v10.14.1 and bind it to the pre-existing environment. The command that i use is:
Assuming that we have created a folder and activated a virtual environment using pipenv shell
inside it and installed nodeenv
nodeenv --node=10.14.1 -p
Check that the environment has installed sucessfully using the command node -v
it should output v10.14.1
If you want to use the isolated version of node that you installed, navigate to the folder in terminal and activate the virtual environment using the command pipenv shell
if you installed node using the command nodeenv -p
it should already be activated, you can check by typing node -v
which will display the version of node.
The beauty of installing an isolated instance of node using nodeenv and pipenv is that you can install different versions of node without creating conflicts. This is especially handy if you have the latest version of node on your computer but come across a legacy project that only works with an older version of node. Nodeenv allows you to create an isolated virtual environment into which you can install the exact version of nodejs you need that does not conflict with your pre-existing node installations.
for more details on nodeenv check out this link [pypi.org/project/nodeenv/]