Fixing NPM install error with node-sass

Step by step guide on fixing NPM install error with node-sass

Mar 18, 2023 | Read time 11 minutes

🔔 Table of contents

Introduction

Recently I was trying to fix a bug with one of my old projects. So I went to git and did a clone and ran

npm install

The project uses node-sass to complile scss files to CSS and came up with the error:

npm ERR! command failed

A more verbose error looks like the following:

npm ERR! code 1
npm ERR! path D:\myproject\node_modules\gulp-sass\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: C:\Program Files\nodejs\node.exe D:\myproject\node_modules\gulp-sass\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
...
...
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@17.3.1 | win32 | x64
npm ERR! gyp verb command rebuild []
npm ERR! gyp verb command clean []
npm ERR! gyp verb clean removing "build" directory
npm ERR! gyp verb command configure []
npm ERR! gyp verb check python checking for Python executable "python2" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\myproject\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\myproject\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\myproject\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:198:21)
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\myproject\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\myproject\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\myproject\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:198:21) {
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp verb `which` failed }
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\myproject\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\myproject\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\myproject\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:198:21)
npm ERR! gyp verb `which` failed  python Error: not found: python
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\myproject\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\myproject\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\myproject\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\myproject\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:198:21) {
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp verb `which` failed }
npm ERR! gyp verb could not find "python". checking python launcher
npm ERR! gyp verb could not find "python". guessing location
npm ERR! gyp verb ensuring that file exists: C:\Python27\python.exe
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
npm ERR! gyp ERR! stack     at PythonFinder.failNoPython (D:\myproject\node_modules\gulp-sass\node_modules\node-gyp\lib\configure.js:484:19)
npm ERR! gyp ERR! stack     at PythonFinder.<anonymous> (D:\myproject\node_modules\gulp-sass\node_modules\node-gyp\lib\configure.js:509:16)
npm ERR! gyp ERR! stack     at D:\myproject\node_modules\graceful-fs\polyfills.js:282:31
npm ERR! gyp ERR! stack     at FSReqCallback.oncomplete (node:fs:198:21)
npm ERR! gyp ERR! System Windows_NT 10.0.19043
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\,yproject\\node_modules\\gulp-sass\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd D:\myproject\node_modules\gulp-sass\node_modules\node-sass
npm ERR! gyp ERR! node -v v17.3.1
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1

Why is node-sass failing to install?

Node-sass is a popular package used in Node.js applications to compile Sass files into CSS. This means that you can specify the .scss file and get it compiled to css.

The main reason why node-sass would be failing is that its been deprecated and the team behind SASS recommends using Dart SASS or sass for short.

Steps to fix this issue

  1. Updgrade node-sass with sass
  2. Make sure you are using a compatible version of node-sass with Node
  3. Make sure to have installed node-sass and its dependencies on your machine
  4. Clear out the node_modules, package-lock.json and install again
  5. Use the flag --unsafe-perm

Tip: The obvious step is to not use node-sass but sass

1. Updgrade node-sass with sass

Node-sass has been deprecated and the team behind it recommends using sass.

We just need to remove it as follows:

npm uninstall node-sass --save

Then install sass:

npm install sass --save

Now if you want to use node-sass for some reason, then follow the next steps.

2. Make sure you are using a compatible version of node-sass with Node

So if you do not want to use the new Dart SASS (sass) and still want to use node-sass, we can change our node version to use a version thats compatible with node-sass

The following table lists the compatible versions of Node with Node-sass:

NodeJS Supported node-sass version Node Module
Node 19 8.0+ 111
Node 18 8.0+ 108
Node 17 7.0+, <8.0 102
Node 16 6.0+ 93
Node 15 5.0+, <7.0 88
Node 14 4.14+ 83
Node 13 4.13+, <5.0 79
Node 12 4.12+, <8.0 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

One good tool to use to manage Node versions is NVM!

To work with NVM and manage your Node versions we can do the following:

  1. For windows, we can go to the binary and install NVM located here:

https://github.com/coreybutler/nvm-windows/tags

For linux distros we can do the following:

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install node 
  1. Uninstall all versions of node with the following command:

nvm uninstall <node version>

  1. Reinstall needed node version:

nvm install <node version>

To get the latest node version we can do nvm install latest

  1. Use node version just installed:

nvm use <node version>

In our case, we can use the command nvm use latest

3. Make sure to have installed node-sass and its dependencies on your machine

node-sass requires a bunch of dependencies and they are required to be installed on your machine first.

In general, make sure that you have installed Python and a build/compiler toolchain. This would vary across systems.

For Unix

  • Python v3.7, v3.8, v3.9, or v3.10
  • make
  • C/C++ compiler toolchain eg GCC

For OSX

  • Python v3.7, v3.8, v3.9, or v3.10
  • XCode Command Line Tools which will install clang, clang++, and make.

On Windows

  • Python v3.7, v3.8, v3.9, or v3.10, head over to the Microsoft Store and download the latest version.

  • Install the Visual C++ Build Environment. You can do this by either getting the “Visual C++ build tools” workload in Visual Studio Build Tools or by using the “Desktop development with C++” workload in Visual Studio Community.

  • Open up cmd and type in “npm config set msvs_version 2017”.

Note: If you’re looking to target native ARM64 Node.js on Windows 10 on ARM, make sure to add the components “Visual C++ compilers and libraries for ARM64” and “Visual C++ ATL for ARM64”.

4. Clear out the node_modules, package-lock.json and install again

When previous steps did not work for you, the final option is to nuke everything.

Open up your terminal and go to the project’s root folder:

  1. Delete node_modules folder. This is where all the packages are saved.
  2. Delete the package-lock.json.
  3. Run npm install

Note: Make sure you are running under administrator priveleges.

5. Use the flag --unsafe-perm

If you know what you are getting into, then try installing node-sass with the --unsafe-perm flag.

This flag allows npm to run scripts with root privileges.

You can install node-sass with the --unsafe-perm flag by running the following command in your terminal:

npm install node-sass --unsafe-perm

Summary

When working on old projects you may come across NPM install errors with node-sass. The main reason this is the case is because node-sass has been deprecated by the team behind it.

It is recommended to use sass instead. If somehow you still need to use node-sass there are a few things to check to get your project up and running:

  • check that the version of node-sass is compatible with your version of Node
  • make sure that you have all the dependencies installed on your machine - eg Python and build/compile tools
  • clear our your node_modules folder and package-lock.json and run the npm install again
  • Try using the --unsafe-perm flag if you know what you are doing!

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube