Menu

Environment variable What, Why, Where and How?

What is environment variable?

Environment variable is variable which holds the path of the software installed in your machine.

Why do we need to set environment variable?

When we compile and run any program, then machine try to find the required compiler or runtime to perform the action which we have requested. So either we to have to specify the directory of the software library every time with our command or we could set it into an environment variable so machine can automatically pickup the library path from there based on the command.
For example:
We set python variable to compile and execute the Python code.
python = C:\Python\Python27\python.exe
We set the JAVA_HOME for compile and run the Java programs.
JAVA_HOME = C:\Program Files\Java\jdk1.8.0_201 
We set MVN_HOME for maven build.
MVN_HOME = D:\apache-maven-3.6.0\bin 

Instead of creating all these variable we could also just add these library paths in a common user variable PATH and later add PATH variable in system variable CLASSPATH.

How to set environment variable?

Right click on my computer icon and select the properties option.

A new window will be get opened. Now click on the advance system settings
advance-system-settings-environment-variable
advance-system-settings-environment-variable

Now click on the environment variables button.
environment-variables
environment-variables
Now here you could either add the installed library path under the PATH user variable to you could create a new user variable and later add the newly create variable in CLASSPATH system variable.
environment-variable-paths.JPG
environment-variable-paths.JPG

Why we need Python to run Node.js?

Node.js is written on c++, and is build on GYP. GYP is a Meta-Build system: a build system that generates other build systems and help us to build the large project that needs multiple platform to build. GYP is developed and written using Python, so to build and run few modules of Node.js we need Python.

Therefore wherever we need node-gyp to build any node module, Python will be required.

References: 

Error: Can't find Python executable "python", you can set the PYTHON env variable.

This error you are getting because npm need Python and your computer doesn't have Python install into it.
So please go ahead and install the Python in your machine, and run the npm install command after installing the Python. 

How to install python?

Go to the Python official website and download the latest release of Python, and simple install it.
Once installation has been successfully completed, then set the python variable in your machine environment variable. To know more, you could see our blog on How to set an environment variable in windows.

Or you could set the variable using this command.
npm config set python C:\Python\Python27\python.exe
If you don't wants to install Python manually and wants all things to be get downloaded by npm itself then you could run npm install -g windows-build-tools, this will download all the required dependencies for you and bring npm in working mode.
To run this command please make sure you have open your node command prompt and cmd with administrative privilege. To do so, go to start and search for node cmd and cmd >> then right click on the application shortcut and use the option run as administrator.
after that just copy and paste the command
npm install -g windows-build-tools

This command will download the Python and install the Python at the below directory in your machine. Go to this path and find the python.exe and set environment variable in CLASSPATH variable.
C:\Users\nodejs\.windows-build-tools\python27\