262

I am getting into a Node.js codebase which requires that I download a few dependencies via NPM, namely jQuery.

In attempting to run npm install jquery, I keep getting this error:

Your environment has been set up for using Node.js 0.8.21 (x64) and NPM

C:\Users\Matt Cashatt>npm install jquery
npm http GET https://registry.npmjs.org/jquery
npm http 304 https://registry.npmjs.org/jquery
npm http GET https://registry.npmjs.org/jsdom
npm http GET https://registry.npmjs.org/xmlhttprequest
npm http GET https://registry.npmjs.org/htmlparser/1.7.6
npm http GET https://registry.npmjs.org/location/0.0.1
npm http GET https://registry.npmjs.org/navigator
npm http GET https://registry.npmjs.org/contextify
npm http 304 https://registry.npmjs.org/htmlparser/1.7.6
npm http 304 https://registry.npmjs.org/xmlhttprequest
npm http 304 https://registry.npmjs.org/location/0.0.1
npm http 304 https://registry.npmjs.org/navigator
npm http 304 https://registry.npmjs.org/jsdom
npm http 304 https://registry.npmjs.org/contextify
npm http GET https://registry.npmjs.org/bindings
npm http GET https://registry.npmjs.org/cssom
npm http GET https://registry.npmjs.org/cssstyle
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/bindings

> contextify@0.1.4 install C:\Users\Matt Cashatt\node_modules\jquery\node_module
s\contextify
> node-gyp rebuild


C:\Users\Matt Cashatt\node_modules\jquery\node_modules\contextify>node "C:\Progr
am Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\b
in\node-gyp.js" rebuild
npm http 304 https://registry.npmjs.org/cssstyle
npm http 304 https://registry.npmjs.org/cssom
npm http 304 https://registry.npmjs.org/request
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT
HON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\nod
e_modules\node-gyp\lib\configure.js:113:14)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node
-gyp\lib\configure.js:82:11
gyp ERR! stack     at Object.oncomplete (fs.js:297:15)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modu
les\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\Matt Cashatt\node_modules\jquery\node_modules\contextify
gyp ERR! node -v v0.8.21
gyp ERR! node-gyp -v v0.8.4
gyp ERR! not ok
npm ERR! error rolling back Error: ENOTEMPTY, rmdir 'C:\Users\Matt Cashatt\node_
modules\jquery\node_modules\jsdom\node_modules\request\tests'
npm ERR! error rolling back  jquery@1.8.3 { [Error: ENOTEMPTY, rmdir 'C:\Users\M
att Cashatt\node_modules\jquery\node_modules\jsdom\node_modules\request\tests']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: 'C:\\Users\\Matt Cashatt\\node_modules\\jque
ry\\node_modules\\jsdom\\node_modules\\request\\tests' }
npm ERR! contextify@0.1.4 install: `node-gyp rebuild`
npm ERR! `cmd "/c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the contextify@0.1.4 install script.
npm ERR! This is most likely a problem with the contextify package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls contextify
npm ERR! There is likely additional logging output above.

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "jquery"
npm ERR! cwd C:\Users\Matt Cashatt
npm ERR! node -v v0.8.21
npm ERR! npm -v 1.2.11
npm ERR! code ELIFECYCLE
npm ERR! Error: ENOENT, lstat 'C:\Users\Matt Cashatt\node_modules\jquery\node_mo
dules\jsdom\node_modules\request\tests\test-pipes.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "jquery"
npm ERR! cwd C:\Users\Matt Cashatt
npm ERR! node -v v0.8.21
npm ERR! npm -v 1.2.11
npm ERR! path C:\Users\Matt Cashatt\node_modules\jquery\node_modules\jsdom\node_
modules\request\tests\test-pipes.js
npm ERR! fstream_path C:\Users\Matt Cashatt\node_modules\jquery\node_modules\jsd
om\node_modules\request\tests\test-pipes.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack C:\Program Files\nodejs\node_modules\npm\node_modules\fst
ream\lib\writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:297:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\Matt Cashatt\npm-debug.log
npm ERR! not ok code 0

C:\Users\Matt Cashatt>

It looks like the failure is due to a missing Python installation. Well, I have installed Python, set the variable, and rebooted and still the error.

Any clue as to what I am missing?

8
  • 1
    More importantly: How did you set the environment variable before rebooting? If you just did PYTHON=C:\Python27\Python.exe in your cmd window and rebooted, the setting was lost.
    – abarnert
    Feb 28, 2013 at 1:56
  • 7
    Also, which Python version did you install? At least older versions of waf and gyp required 2.x but didn't say anything about it, and would give all kinds of mysterious errors if you installed 3.x instead.
    – abarnert
    Feb 28, 2013 at 1:57
  • Thanks for your comments. I have posted the error in text format. I am using v2.7 Feb 28, 2013 at 1:58
  • OK, but please answer the "more importantly" question, because that's, as the text implies, important. You may also want to test this yourself: In the cmd.exe window, before trying the npm command, does either python or %PYTHON% start a Python interpreter? If not, you haven't set it up right.
    – abarnert
    Feb 28, 2013 at 1:58
  • set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib Feb 28, 2013 at 1:59

27 Answers 27

519

If you haven't got python installed along with all the node-gyp dependencies, simply open Powershell or Git Bash with administrator privileges and execute:

npm install --global --production windows-build-tools

and then to install the package:

npm install --global node-gyp

once installed, you will have all the node-gyp dependencies downloaded, but you still need the environment variable. Validate Python is indeed found in the correct folder:

C:\Users\ben\.windows-build-tools\python27\python.exe 

*Note - it uses python 2.7 not 3.x as it is not supported*

If it doesn't moan, go ahead and create your (user) environment variable:

setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"

restart cmd, and verify the variable exists via set PYTHON which should return the variable ($env:PYTHON if using Powershell)

Lastly re-apply npm install <module>

8
  • 1
    I had some permissions error, but managed to manually install python from: C:\Users[me]\.windows-build-tools\python.msi . In the installation is an option to add it to the path. (Restarting the cmd/PS ) and it worked
    – d.raev
    Aug 12, 2017 at 9:46
  • I get the error dh key too small : openssk\ssl\s3_clnt,c:3641 Sep 13, 2017 at 6:58
  • 20
    Installing windows-build-tools required PowerShell run as Admin on Windows 10. This means the setx command becomes setx PYTHON $env:USERPROFILE\.windows-build-tools\python27\python.exe
    – Bae
    Oct 30, 2017 at 23:43
  • I did have to run these commands in an elevated command prompt, but it seems to have worked. Manually installing Python 3.x and setting the PYTHON environment variable to point to that did not work. I assume my error related to the difference in python versioning? Thanks regardless! Jun 6, 2018 at 15:47
  • 3
    The whole point of dependency management is so we don't have to do stuff like this... smh Jun 8, 2021 at 21:06
144

Your problem is that you didn't set the environment variable.

The error clearly says this:

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

And in your comment, you say you did this:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

That's nice, but that doesn't set the PYTHON variable, it sets the PYTHONPATH variable.


Meanwhile, just using the set command only affects the current cmd session. If you reboot after that, as you say you did, you end up with a whole new cmd session that doesn't have that variable set in it.

There are a few ways to set environment variables permanently—the easiest is in the System Control Panel in XP, which is of course different in Vista, different again in 7, and different again in 8, but you can google for it.

Alternatively, just do the set right before the npm command, without rebooting in between.


You can test whether you've done things right by doing the exact same thing the config script is trying to do: Before running npm, try running %PYTHON%. If you've done it right, you'll get a Python interpreter (which you can immediately quit). If you get an error, you haven't done it right.


There are two problems with this:

set PYTHON=%PYTHON%;D:\Python

First, you're setting PYTHON to ;D:\Python. That extra semicolon is fine for a semicolon-separated list of paths, like PATH or PYTHONPATH, but not for a single value like PYTHON. And likewise, adding a new value to the existing value is what you want when you want to add another path to a list of paths, but not for a single value. So, you just want set PYTHON=D:\Python.

Second, D:\Python is not the path to your Python interpreter. It's something like D:\Python\Python.exe, or D:\Python\bin\Python.exe. Find the right path, make sure it works on its own (e.g., type D:\Python\bin\Python.exe and make sure you get a Python interpreter), then set the variable and use it.


So:

set PYTHON=D:\Python\bin\Python.exe

Or, if you want to make it permanent, do the equivalent in the Control Panel.

6
  • 1
    OK, so I get this:Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Matt Cashatt>set PYTHON=%PYTHON%;D:\Python C:\Users\Matt Cashatt>%PYTHON% '%PYTHON%' is not recognized as an internal or external command, operable program or batch file. C:\Users\Matt Cashatt> Feb 28, 2013 at 2:05
  • 1
    Also, the D drive is where I have Python in case you are wondering. Thanks again for your help. Feb 28, 2013 at 2:07
  • Worked like a charm, thanks! For others: I had to run the set command in the NPM command window for some reason and then run the mpm install jquery command right after that. If I did them separately it didn't work for some reason. Feb 28, 2013 at 3:05
  • @MatthewPatrickCashatt: As I explained, the set command only affects the current cmd window, so you have to do it before running npm in the same window. If you want to set environment variables more permanently, that's in the System Control Panel, or equivalent for your version of Windows.
    – abarnert
    Feb 28, 2013 at 18:21
  • 3
    @abarnert is the actual answer to this question then set PYTHON=D:\Python\bin\Python.exe - would be helpful if you gave the correct way as well as explaining the wrong way Feb 10, 2014 at 10:34
26

For me after installing windows-build-tools with the below comment

npm --add-python-to-path='true' --debug install --global windows-build-tools

running the code below

npm config set python "%USERPROFILE%\.windows-build-tools\python27\python.exe"

has worked.

1
  • I also needed both of the above steps. I'll also add that, for me, (Windows 10 Pro 1803, node v10.15.3, npm v6.9.0) when I ran the first step, the 'npm install windows-build-tools' step, that install never seemed to complete, it seemed to be looping endlessly (feeding back the same output lines to the console). After watching this seeming endless loop for a number of minutes, I elected to use Ctrl+C to "end the batch job". Everything worked as intended, interrupting the loop didn't appear to be a problem. Apr 8, 2019 at 6:06
14

Here is a guide that resolved a lot of these issues for me.

http://www.steveworkman.com/node-js/2012/installing-jsdom-on-windows/

I remember in particular the python version as important. Make sure you install 2.7.3 instead of 3's.

2
  • 4
    The link is unfortunately broken. May 6, 2020 at 7:44
  • @JuanguiJordán In the meantime, the required python version is python3
    – lcnittl
    Oct 9, 2020 at 7:27
11

One and/or multiple of those should help:

  1. Add C:\Python27\ to your PATH variable (considering you have Python installed in this directory)
    How to set PATH env variable: http://www.computerhope.com/issues/ch000549.htm
    Restart your console and/or Windows after setting variable.

  2. In the same section as above ("Environment Variables"), add new variable with name PYTHON and value C:\Python27\python.exe
    Restart your console and/or Windows after setting variable.

  3. Open Windows command line (cmd) in Admin mode.
    Change directory to your Python installation path: cd C:\Python27
    Make symlink needed for some installations: mklink python2.7.exe python.exe

Please note that you should have Python 2.x, NOT 3.x, to run node-gyp based installations!

The text below says about Unix, but Windows version also requires Python 2.x:

You can install with npm:

$ npm install -g node-gyp
You will also need to install:

On Unix:
python (v2.7 recommended, v3.x.x is not supported)
make
A proper C/C++ compiler toolchain, like GCC

This article may also help: https://github.com/nodejs/node-gyp#installation

9

This helped: https://www.npmjs.com/package/node-gyp

I followed these steps:

npm install -g node-gyp

then:

npm install --global --production windows-build-tools
1
8

I had the same issue and none of these answers did help. In my case PYTHON variable was set correctly. However python was installed too deep, i.e. has too long path. So, I did the following:

  1. reinstalled python to c:\python
  2. set environmental variable PYTHON to C:\python\python.exe

And that’s it!

8

I met the same challenge while trying to install node-sass@4.9.4.

And after looking at the current official documentation, and having read the answers above, i noticed that you might not necessarily have to install node-gyp nor install windows-build tools. This is what it says, here about installing node-gyp on windows. Remember node-gyp is involved in the installation process of node-sass. And you don't really have to re-install another python version.

This is the savior, configure the python path that "npm" should look for while installing any packages that require build-tools.

C:\> npm config set python /Python36/python

I had installed python3.6.3, on windows-7, there.

4
  • Same solution worked for me while installing node-sass@4.9.4, when i switched to windows-10, using node@10.6.3 and yarn@1.19.1
    – MwamiTovi
    Nov 19, 2019 at 13:14
  • Note that Python36/python should be the path to your python executable file .exe
    – MwamiTovi
    Nov 25, 2019 at 17:41
  • If new, goto where your python executable was installed. In windows, Press hold Shift, and Right-Click and enter this: npm config set python
    – MwamiTovi
    Jan 29, 2020 at 9:09
  • 1
    2022, Windows 10, this worked! Thanks
    – sylargaf
    Jun 28, 2022 at 14:42
7

there are some solution to solve this issue : 1 ) run your command prompt as "administrator".

if first solution doesn't solve your problem try this one :

2 ) open a command prompt as administrator paste following line of code and hit enter :

npm install --global --production windows-build-tools
6

The following worked for me from the command line as admin:

Installing windows-build-tools (this can take 15-20 minutes):

 npm --add-python-to-path='true' --debug install --global windows-build-tools

Adding/updating the environment variable:

setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"

Installing node-gyp:

npm install --global node-gyp

Changing the name of the exe file from Python to Python2.7.

C:\Users\username\.windows-build-tools\python27\Python2.7

npm install module_name --save

6

This is most easiest way to let NPM do everything for you

npm --add-python-to-path='true' --debug install --global windows-build-tools
5

TL;DR Make a copy or alias of your python.exe with name python2.7.exe

My python 2.7 was installed as

D:\app\Python27\python.exe

I always got this error no matter how I set (and verified) PYTHON env variable:

gyp ERR! stack Error: Can't find Python executable "python2.7", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:103:14)

The reason for this was that in node-gyp's configure.js the python executable was resolved like:

var python = gyp.opts.python || process.env.PYTHON || 'python'

And it turned out that gyp.opts.python had value 'python2.7' thus overriding process.env.PYTHON.

I resolved this by creating an alias for python.exe executable with name node-gyp was looking for:

D:\app\Python27>mklink python2.7.exe python.exe

You need admin rights for this operation.

5

i can't help but to mention this. If you're using Python3 and failing with node-gyp, then i'm sad to tell you node-gyp currently doesn't support python3.

Here is a link for you: https://github.com/nodejs/node-gyp/issues/1268 https://github.com/nodejs/node-gyp/issues/193

1
  • Not anymore, luckily.
    – lcnittl
    Oct 9, 2020 at 7:30
4

The right way is 1) Download and Install python 2.7.14 from here. 2) Set environment variable for python from here.

done!

note: Please set environment variable accordingly. I answered here for windows.

3

For me, these steps fixed the issue:

1- Running this cmd as admin:

npm install --global --production windows-build-tools

2- Then running npm rebuild after the 1st step is completed (especially completing the python 2.7 installation, which was the main cause of the issue)

2

Why not downloading the python installer here ? It make the work for you when you check the path installation

2

I tried all the above steps, none worked for me. I was trying to install the argon2 npm package via yarn on a windows 10 system.

so this is what finally worked

  1. I ran npm --add-python-to-path='true' --debug install --global windows-build-tools to install python as an admin in command terminal the above command ran into an infinite loop which you can terminate by using Ctrl + C.
  2. I set the environment variable by running this setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"
  3. I installed node-gyp globally by running this command
    npm install --global node-gyp
  4. I downloaded VS 2019 community edition installer, while installing I added desktop development with C++ as shown in the image enter image description here
  5. I ran the yarn command again. for e.g yarn add argon2

I hope these steps help you out. it took me a few hours to find this solution

1

Here is the correct command: set path=%path%;C:\Python34 [Replace with the correct path of your python installation]

I had the same problem and I just solved this like that.

As some other people pointed out, this is volatile configuration, it only works for the current cmd session, and (obviously) you have to set your path before you run npm install.

I hope this helps.

1

gyp ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT HON env variable.

Not necessary to reinstall, this exception throw by node-gyp script, then try to rebuild. It's enough setup environment variable like in my case I did:

SET PYTHON=C:\work\_env\Python27\python.exe
1

If you're trying to use this on Cygwin, then you need to follow the instructions in this answer. (It's a problem how Cygwin treats Windows symlinks.)

1

Example : pg_config not executable / error node-gyp

Solution : On windows just try to add PATH Env -> C:\Program Files\PostgreSQL\12\bin

Work for me, Now i can use npm i pg-promise for example or other dependencies.

1
  • For pg-promise you do not need this. You probably were caught up by this. Just use the latest version, and you will be fine, no need for any of that extra stuff ;)
    – vitaly-t
    Nov 12, 2019 at 15:02
1

What worked for me:

  • Go here and install the needed python version
  • Run the following command using a cmd npm config set python C:\Users\jorge\AppData\Local\Programs\Python\Python310\python.exe

Change the path to your directory

0

For me, The issue was that i was using node's latest version and not the LTS version which is the stable version and recommended for most users.
Using the LTS Version solved the issue.
You can download from here :

LTS Version

Current Latest Version

0

Set path like below and it will work

> set PYTHON=D:\\ranjith\\installed\\python-3.6.4\\python.exe

> npm config set python D:\\ranjith\\installed\\python-3.6.4\\python.exe

Then build your project (for me like)

> yarn build
0

I was facing same error while trying to execute "npm install" after downloading Akveo ngx-admin template.

It was kind of a weird situtation because I just start up the same template in another laptop. I realized that I had antoher versión of npm installed.

enter image description here

As you can see, my node version is "14.17.0" and at first step my npm version was "7.13.0". Then I realized that the version of npm in my other laptop was "6.14.11", so I decided to downgrade the npm version by executing "npm install -g npm@6.14.11".

Then the "npm install" worked very well.

Note: No need to install windows-build-tools or any python version.

0

Downloaded the latest version from https://www.python.org/downloads/ While installing checked the Add Python 3.10 to PATH option. After a reboot everything was working perfectly.

-1

The answer to my problem was easy one.

I had left a Windows command window open during the Python install.

After the Python install, I attempted to launch Python from that window.

The solution for me was easy.

When I closed the command window and opened a new command window, the problem went away.

I had to open a new command window so that the path environment variable would contain the path to Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.