npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
5 min

banner

# npm ERR! gyp ERR! stack Error: make failed with exit code: 2

The error "npm ERR! gyp ERR! stack Error: make failed with exit code: 2" occurs when there's a glitch in your package-lock.json file or you try to install an outdated or incompatible version of an NPM package.

To solve the error, delete your node_modules and package-lock.json file and rerun the npm install command.

shell
โ›”๏ธ remote: gyp ERR! build error remote: gyp ERR! stack Error: make failed with exit code: 2 remote: gyp ERR! stack at ChildProcess.onExit remote: gyp ERR! stack at ChildProcess.emit (node:events:390:28) remote: gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12) remote: gyp ERR! remote: gyp ERR! command "/tmp/build_47b2dcad/bin/node" "/tmp/build_47b2dcad/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" remote: gyp ERR! cwd /tmp/build_47b2dcad/node_modules/node-sass remote: gyp ERR! node -v v16.13.1 remote: gyp ERR! node-gyp -v v3.8.0 remote: gyp ERR! not ok remote: Build failed with error code: 1

The first thing you should try is to reinstall your node_modules.

# Delete your node_modules and reinstall your dependencies

Open your terminal in your project's root directory (where your package.json file is) and delete your node_modules and package-lock.json (not package.json) files, rerun npm install and restart your dev server.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force npm install

Make sure to restart your IDE and dev server if the error persists.

If you are on Linux, make sure you have the meta-packages installed by running the command that's specific to your operating system.

shell
# ๐Ÿ‘‡๏ธ Debian (Ubuntu) sudo apt install -y build-essential # ๐Ÿ‘‡๏ธ CentOS, RHEL sudo yum install gcc gcc-c++ make # ๐Ÿ‘‡๏ธ Fedora sudo dnf groupinstall "Development Tools" "Development Libraries"

Try to rerun your npm install command after installing the build tool packages and restart your development server.

If you are on macOS or Linux, try to delete your ~/.node-gyp folder and rerun npm install.

shell
rm -rf ~/.node-gyp npm install

The error also commonly occurs when using the node-sass package, if you don't use node-sass, scroll down to the next subheading.

# Use the sass package instead of the deprecated node-sass package

If you didn't get the error when trying to install node-sass scroll down to the next subheading.

If you got the error when installing the node-sass package, install the sass package instead because node-sass has been deprecated.

Open your terminal in your project's root directory (where your package.json file is) and run the following commands.

shell
# ๐Ÿ‘‡๏ธ with NPM npm uninstall node-sass npm install sass --save-dev # ๐Ÿ‘‡๏ธ or with YARN yarn remove node-sass yarn add sass --dev

The two commands uninstall the deprecated node-sass module and install the sass module.

Try to restart your development server after running the commands.

Alternatively, you can install a compatible version of node-sass.

If you'd rather use the deprecated node-sass module, you have to install a version of node-sass that is compatible with your Node.js version.

The table in this section of the node-sass npm page shows which node-sass version you should install for your version of Node.js.

You can use the node --version command to get your version of Node.js.

shell
node --version

get node version

Here is part of the table.

NodeJS versionSupported node-sass version
Node 198.0+
Node 188.0+
Node 177.0+, <8.0
Node 166.0+
Node 155.0+, <7.0
Node 144.14+
Node 134.13+, <5.0

For example, if your Node.js version is 17, you would install version 7 of node-sass.

shell
# ๐Ÿ‘‡๏ธ if your Node.js version is 17 npm install node-sass@7 --save-dev --legacy-peer-deps # ๐Ÿ‘‡๏ธ for YARN with Node.js version 17 yarn add node-sass@7 --dev

Your node-sass version has to be compatible with your Node.js version, otherwise, the error occurs.

You can check your node-sass version in the devDependencies section of your package.json file.

# Update the version of your NPM packages

If you have a recent version of Node.js installed and your package.json file contains older versions of packages that are not compatible with your Node.js version, the error occurs.

You can use the npm-check-updates package to update your package.json dependencies to the latest version.

Open your terminal in your project's root directory (where your package.json file is) and run the following command.

You can add your package.json file to version control (e.g. git) because the following 2 commands will update the versions of your packages in your package.json file.
shell
npx npm-check-updates -u npm install

If the error persists, try to delete node_modules and package-lock.json and reinstall.

# Try to reinstall your node_modules

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, rerun npm install and restart your IDE.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error persists.

# Try to update your npm version

Try to update your NPM version by running the following command.

shell
npm install -g npm@latest # ๐Ÿ‘‡๏ธ If you get a permissions error on macOS / Linux sudo npm install -g npm@latest

update npm version

If you get a permissions error on Windows, open CMD as an administrator and rerun the command.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Rerun the command.
shell
npm install -g npm@latest npm install -g npm@latest --force

Try to run the npm install command after updating your npm version.

If the error persists, try to install the long-term supported version of Node.js.

# Update your Node.js version to the long-term supported one

You might be using a Node.js version that is not supported by the packages you're trying to install.

This can happen when you use a Node.js version that is too recent, so support hasn't been added or a Node.js version that is out of date and is no longer supported by the packages you're trying to install.

You can check your Node.js version with the node --version command.

You can use the node --version command to get your Node.js version.

shell
node --version

get node version

If you use nvm, you can update your version of Node.js and npm with the following 2 commands.

shell
nvm install --lts nvm use --lts

install lts node js version

You can also use the n package to manage your Node.js version.

shell
npm cache clean -f npm install -g n n stable

If you get a permissions error when issuing the commands, open CMD as an administrator (Windows) or prefix the commands with sudo (macOS and Linux).

shell
sudo npm cache clean -f sudo npm install -g n sudo n stable

Alternatively, you can download the long-term supported version of Node.js from the official nodejs.org website.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev