Node.js: Python not found exception due to node-sass and node-gyp

node.jsNode GypNode Sass

node.js Problem Overview


Suddenly in one of my jenkins environment build has started failing, while in local machine it seems to be working fine as i have python installed,

From the logs i was able to detect that the problem is with internal dependency that is node-gyp v3.5.0 from node-sass v3.8.0 when i researched by visiting this about node-gyp and found the prerequisite that Python needs to be installed.

So my question is that what version of node-sass can i install to bypass this or is there a better solution as my build was running fine till this morning on the same environment.

Node v5.10.1

ERROR LOG

gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:80:29)
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:89:16
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\index.js:44:5
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\windows.js:29:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:82:15)
gyp verb `which` failed  python2 { [Error: not found: python2] code: 'ENOENT' }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` failed Error: not found: python
gyp verb `which` failed     at getNotFoundError (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:80:29)
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:89:16
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\index.js:44:5
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\windows.js:29:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:82:15)
gyp verb `which` failed  python { [Error: not found: python] code: 'ENOENT' }
gyp verb could not find "python". checking python launcher 
gyp verb could not find "python". guessing location 
gyp verb ensuring that file exists: C:\Python27\python.exe
gyp ERR! configure error 
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at Object.failNoPython (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\node-gyp\lib\configure.js:454:19)
gyp ERR! stack     at Object.<anonymous> (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\node-gyp\lib\configure.js:480:16)
gyp ERR! stack     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:82:15)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files (x86)\\Jenkins\\jobs\\NdbSite-hot-fix-Manual-PreBuild\\workspace\\src\\NdbSite.UI\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\node-sass
gyp ERR! node -v v5.10.1
gyp ERR! node-gyp -v v3.5.0
gyp ERR! not ok 
Build failed

Any thoughts are much appreciated, Thanks.

node.js Solutions


Solution 1 - node.js

so this happened to me on windows recently. I fix it by following the following steps using a PowerShell with admin privileges:

  1. delete node_modulesfolder
  2. running npm install --global windows-build-tools with administrative privilege. (in my case need restart - and restart without ask!!!)
  3. reinstalling node modules or node-sass with npm install

Solution 2 - node.js

The error message means that it cannot locate your python executable or binary.

In many cases, it's installed at c:\python27. if it's not installed yet, you can install it with npm install --global windows-build-tools, which will only work if it hasn't been installed yet.

Adding it to the environment variables does not always work. A better alternative, is to just set it in the npm config.

npm config set python c:\python27\python.exe

Solution 3 - node.js

You need to follow the below steps to fix the error:

    1: DELETE node_modules
    2: npm install node-sass@<version-as-per-below-table>
    3. npm install

Every node-sass version corresponds to a specific Node.JS build.

Below is a quick guide for minimum and maximum support supported version of node-sass (Check updates to below table here) :


| NodeJS  | Supported node-sass version | Node Module |
|---------|-----------------------------|-------------|
| Node 16 | 6.0+                        | 93          |
| Node 15 | 5.0+                        | 88          |
| Node 14 | 4.14+                       | 83          |
| Node 13 | 4.13+, <5.0                 | 79          |
| Node 12 | 4.12+                       | 72          |
| Node 11 | 4.10+, <5.0                 | 67          |
| Node 10 | 4.9+, <6.0                  | 64          |
| Node 8  | 4.5.3+, <5.0                | 57          |
| Node <8 | <5.0                        | <57         |

Solution 4 - node.js

My machine is Windows 10, I've faced similar problems while tried to compile SASS using node-sass package. My node version is v10.16.3 and npm version is 6.9.0

The way that I resolved the problem:

  1. At first delete package-lock.json file and node_modules/ folder.
  2. Open Windows PowerShell as Administrator.
  3. Run the command npm i -g node-sass.
  4. After that, go to the project folder and run npm install
  5. And finally, run the SASS compiling script, in my case, it is npm run build:css

And it works!!

Solution 5 - node.js

Node-sass tries to download the binary for you platform when installing. Node 5 is supported by 3.8 https://github.com/sass/node-sass/releases/tag/v3.8.0 If your Jenkins can't download the prebuilt binary, then you need to follow the platform requirements on Node-gyp README (Python2, VS or MSBuild, ...) If possible I'd suggest updating your Node to at least 6 since 5 isn't supported by Node anymore. If you want to upgrade to 8, you'll need to update node-sass to 4.5.3

Solution 6 - node.js

I had to:

Delete node_modules
Uninstall/reinstall node
npm install node-sass@4.12.0

worked fine after forcing it to the right sass version, according to the version said to be working with the right node.

NodeJS	Minimum node-sass version	Node Module
Node 12	4.12+	72
Node 11	4.10+	67
Node 10	4.9+	64
Node 8	4.5.3+	57

There was lots of other errors that seemed to be caused by the wrong sass version defined.

Solution 7 - node.js

Two options (December 2021):

  • 1 - If you can't upgrade the node-sass package!
    • Install Python 2 and modify its .exe file name.
    • Fix PATH env variable records order (details below).
  • 2 - Update node-sass package.

Precondition

You updated Node.JS to the latest version (v16.13.1) on Windows 10.
You run yarn install or npm install in your project's folder and receive an error (look below).

Option 1. Installing of Python-2 and fixing PATH env

Latest Python 2 version

See MSI installer enter image description here

After installation Python installations

Note! Try to reinstall the latest Node.js to x86 from x64, if you get the following error on x64. (But with x86 you could receive memory limit errors, also.)

Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (93)
Resort Python records in PATH env variable

Python 3 records must be above Python 2 records!

PATH environment variable editing

Sorting of Python records in PATH environment variable

Copy python.exe to python2.exe

Renaming of python.exe to python2.exe

Option 2. Upgrading of the node-sass package

Over package.json file

Modify package.json and run yarn install or npm install on your project's folder.
Other files will be updated automatically.

Before befor updating before updating in package.json

After updating: After updating

Or use yarn or npm CLI to do the same but it may noticeably change your .lock files.

After updating you could realize that you need to update also sass-loader, webpack, etc.

Error you may have (FYI)

gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2

gyp ERR! stack Error: Command failed: C:\Python310\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Solution 8 - node.js

node-gyp requires old Python 2 - link

If you don't have it installed - check other answers about installing windows-build-tools.

If you are like me and have both old and new Python versions installed, chances are that node-gyp tries to use Python 3. And that results in the following SyntaxError: invalid syntax error.

I found an article about having two Python versions installed. And they recommend renaming Python 2.* executable to python2.exe - link.

So it looks like node-gyp is expecting to find old Python 2 executable renamed. Hence the error message:

...
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
...

Once I renamed C:\Python27\python.exe to C:\Python27\python2.exe it worked without errors.

Of course, both C:\Python27\ and C:\Python39\ have to be in PATH variable. And no need in setting old Python version in npm config. Your default Python still will be the new one.

Solution 9 - node.js

My answer might not apply to everyone. Node version: v10.16.0 NPM: 6.9.0

I was having a lot of trouble using node-sass and node-sass-middleware. They are interesting packages because they are widely used (millions of downloads weekly), but their githubs show a limited dependencies and coverage. I was updating an older platform I'd been working on.

What I ended up having to do was:

1) Manually Delete node_modules

2) Manually Delete package-lock.json

3) sudo npm install node-sass --unsafe-perm=true --allow-root

4) sudo npm install node-sass-middleware --unsafe-perm=true --allow-root

I had the following help, thanks!

https://stackoverflow.com/questions/52164351/pre-built-binaries-not-found-for-grpc1-10-1-and-node10-9-0

https://stackoverflow.com/questions/35954725/error-eacces-permission-denied-when-trying-to-install-eslint-using-npm

Solution 10 - node.js

I'm on a Windows 11 machine, and to fix it I downloaded python27 myself, using this link: https://www.python.org/download/releases/2.7/.

And after installed I ran this command:

npm config set python c:\python27\python.exe

Solution 11 - node.js

This is 2 years old, but none of them helped me.

I uninstalled my NodeJS v12.8.1 (Current) and installed a brand new v10.16.3 (LTS) and my ng build --prod worked.

Solution 12 - node.js

Hey I got this error resolved by following the steps

  • first I uninstalled python 3.8.6 (latest version)
  • then I installed python 2.7.1 (any Python 2 version will work, but not much older and this is recommended)
  • then I added c:\python27 to environment variables
  • my OS is windows, so I followed this link
  • It worked

Solution 13 - node.js

I had node 15.x.x , and "node-sass": "^4.11.0". I saw in the release notes from node-sass and saw the node higest version compatible with node-sass 4.11.0 was 11, so I uninstalled node and reinstall 11.15.0 version (I'm working with Windows). Check node-sass releases. (this is what you should see in the node-sass releases.)

Hope that helps and sorry for my english :)

Solution 14 - node.js

Here is what helped me to prevent the error Error: not found: python2 when running a project based on the old node-gyp v3.8.0.

  1. Run npm install --global --production [email protected]
  2. Rename python.exe to python2.exe in %USERPROFILE%\.windows-build-tools\python27
  3. Make sure that %USERPROFILE%\.windows-build-tools\python27 is part of your PATH environment variable (this is what windows-build-tools should already do for you)
  4. Download Visual Studio 2017 Build Tools
  5. Tell npm to use VS 2017 with npm config set msvs_version 2017
  6. Run the script which uses node-gyp (node-gyp will now find python2.exe)

Alternative: Force upgrade of node-gyp

You can also try to upgrade your project to use node-gyp v8.0.0 instead of node-gyp v3.8.0. In this case, add the following resolution entry to your package.json:

  "resolutions": {
    "node-gyp": "8.0.0"
  }

Afterwards, make sure that you have Python 3 installed and Visual Studio 2017 Build Tools and/or Visual Studio 2019 Build Tools. You can also run npm config delete msvs_version to let node-gyp automatically detect your VS version.

Tested with:

  • Windows 10 Pro Build 19042.985
  • Node.js v15.14.0
  • npm v7.7.6
  • node-gyp -v v3.8.0.

Solution 15 - node.js

had the same issue lost hours trying to install different version of python on my PC. Simply Upgrade node to the latest version v8.11.2 and npm 5.6.0, then after install [email protected] and you'll be fine.

Solution 16 - node.js

We encountered this problem in 2021 when setting up a new TeamCity build agent. Originally we had installed nodejs using Chocolatey. We reinstalled going back to the original installer node-v12.12.0-x64.msi. The third screen in the installer dialog offers to install the tools necessary to compile native modules:

enter image description here

After doing this our problem was gone.

Solution 17 - node.js

Worked for me in 2022, actually we can install python 2.7 through npm After the installation for python 2.7, we can force to run npm install with using python 2.7

Sass

  1. npm install --global windows-build-tools --save
  2. npm install --python=python2.7

Solution 18 - node.js

After looking at all the answers, i notice this solution might be very helpful. It explains how to configure "npm" to find your installed python version while installing node-sass. Remember, node-sass requires node-gyp (an npm build-tool) which looks for your python path. Or just install python, and follow the same solution given (in the link). Thanks.

Solution 19 - node.js

In case of yarn projects, ignoring the node version with "yarn install --ignore-engines" will install any node-sass v. without these python errors

Solution 20 - node.js

Python2 maintenance status is "end of life" so it not installed by default on new ubuntu releases.

Installing the latest python2.7 worked fine for me.

Here is the instructions to install it:

tar -xf Python-2.7.18.tgz
cd Python-2.7.18
./configure --enable-optimizations
sudo make altinstall

Open a new terminal and try agin the npm install

Solution 21 - node.js

node-sass is deprecated as of October 26th, 2020

No need to mess around with installing old versions of Python, just migrate to dart.sass

For my ReactJS project (npm), the following steps worked for me:

  1. Delete node_modules from the project

  2. In package.json, remove this dependency:

    "dependencies": {,
        "node-sass": "^3.0.0",}
    
  3. npm install --save-dev sass

  4. npm install

  5. Now try running your project using npm start

  6. If you receive an error like "To import Sass files, you first need to install node-sass", run this command as described here:
    npm install node-sass@npm:sass

If you're using yarn, do the equivalent commands but on the last step do this instead, as described here:
yarn remove node-sass
yarn add sass


Solution 22 - node.js

I found the same issue with Node 12.19.0 and yarn 1.22.5 on Windows 10. I fixed the problem by installing latest stable python 64-bit with adding the path to Environment Variables during python installation. After python installation, I restarted my machine for env vars.

Solution 23 - node.js

In my case, i had node version v16 installed in the machine, but the react project used node-sass version v4.x.x, Updating the package.json with node-sass version to v6.0.1 solved the issue,
node version support for node-sass can be checked from official npm page: https://www.npmjs.com/package/node-sass

Solution 24 - node.js

also you can try to use sass module instead of node-sass:

npm install sass

Solution 25 - node.js

If you are using Visual Studio. follow these steps:

npm cache clean --force
npm config set msvs_version 2019 --global
npm install node-sass

Solution 26 - node.js

This same issue I faced few days ago. Believe on me their is no need to install or uninstall other dependencies in your project or system.

How had I resolve this issue let me share my experience.

  • Please go to package.json file
  • Scroll down all the way below or search for optionalDependencies Object
  • Just remove this whole object & save the file
  • try npm install, it will work

Another Possible Solution:

Please check your package.json file if node-sass element is exist inside the optionalDepedencies Object then you need to run npm command in this way npm install --no-optional or also If just remove node-sass element from optionalDepedencies Object

Note: this solution is only for Angular Project

enter image description here Please Check out this screenshot of package.json file

Solution 27 - node.js

Downgrading to node 10.24.1 version with npm 6.14.12 worked for me. Along with installing windows-build-tools using

> npm i -g windows-build-tools

Solution 28 - node.js

I am using Windows 11 and I had tried using npm i -g windows-build-tools but it stuck forever. So, what worked for me was:

  1. Install Visual Studio Build tools 2017 from here.
  2. After installing, open Powershell as admin.
  3. Enter this command: npm install --global --production windows-build-tools --vs2015
  4. Thank me later!

It fixed all errors related to build tools for me on Windows 11.

Solution 29 - node.js

npm i npm -g will work if node version is updated. I ran into this issue, and npm install -g --production windows-build-tools worked as interim solution. But upgrading Node.js, NPM, and associated package versions is a better long term option.

Solution 30 - node.js

My OS is Win 10 x64. The similar issue found while doing npm install.

  1. Check that the node version suits the node-sass version.(Refer to the above tables.) Upgrade or downgrade node version, if needed

  2. Delete node_modules folder

    $ npm install node-sass@

    $ npm install

Well done.

Solution 31 - node.js

go to package.json and change

"node-sass": "4.14.1",

with

"node-sass": "^7.0.1",

it works for me after that i can able to run npm install

in other word you need to use then latest package which can support the latest python version.

Note: Node Saas 7.0.1 is compatible with "react-scripts": "5.0.0" if your package.json has "react-scripts": "4.0.0", then change it as

 "react-scripts": "5.0.0", 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVipul PanthView Question on Stackoverflow
Solution 1 - node.jsShezan KaziView Answer on Stackoverflow
Solution 2 - node.jsbvdbView Answer on Stackoverflow
Solution 3 - node.jsGopal MishraView Answer on Stackoverflow
Solution 4 - node.jsFatema Tuz ZuhoraView Answer on Stackoverflow
Solution 5 - node.jsnschonniView Answer on Stackoverflow
Solution 6 - node.jsStephen HimesView Answer on Stackoverflow
Solution 7 - node.jsit3xlView Answer on Stackoverflow
Solution 8 - node.jsKonstantin LyakhView Answer on Stackoverflow
Solution 9 - node.jsmaudulusView Answer on Stackoverflow
Solution 10 - node.jsTiago ÁvilaView Answer on Stackoverflow
Solution 11 - node.jsBruno MiquelinView Answer on Stackoverflow
Solution 12 - node.jsRohan DevakiView Answer on Stackoverflow
Solution 13 - node.jsagustinrodriguez7View Answer on Stackoverflow
Solution 14 - node.jsBenny NeugebauerView Answer on Stackoverflow
Solution 15 - node.jsdavyCodeView Answer on Stackoverflow
Solution 16 - node.jsRoss PresserView Answer on Stackoverflow
Solution 17 - node.jsW KennyView Answer on Stackoverflow
Solution 18 - node.jsMwamiToviView Answer on Stackoverflow
Solution 19 - node.jsRegeView Answer on Stackoverflow
Solution 20 - node.jsConstantin De La RocheView Answer on Stackoverflow
Solution 21 - node.jsAlex ErtlView Answer on Stackoverflow
Solution 22 - node.jsDevLoverUmarView Answer on Stackoverflow
Solution 23 - node.jskritizView Answer on Stackoverflow
Solution 24 - node.jsIvan BryzzhinView Answer on Stackoverflow
Solution 25 - node.jsSharpCoderView Answer on Stackoverflow
Solution 26 - node.jsSuneel KumarView Answer on Stackoverflow
Solution 27 - node.jsRahulView Answer on Stackoverflow
Solution 28 - node.jshighDef4kView Answer on Stackoverflow
Solution 29 - node.jsCodeipView Answer on Stackoverflow
Solution 30 - node.jssmaster0517View Answer on Stackoverflow
Solution 31 - node.jsHassan SaeedView Answer on Stackoverflow