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

Introduction

Welcome to this Byte! We're going to take a closer look at the infamous npm ERR! gyp ERR! stack Error: 'make' failed with exit code: 2. This error message might seem like a jumble of words and symbols, but it's actually a cry for help from your npm installation. We'll dissect this error, understand its root cause, and then walk you through how to fix it.

Why do we get this error?

A lot can go wrong during an install with npm, primarily because there are a lot of packages that are built during install using tools like node-gyp. One common error you might run into is make failed with exit code: 2

This error is usually thrown when npm is trying to build a package that includes native C++ code. npm uses a tool called node-gyp to compile and build these native modules. If node-gyp encounters a problem during the build process, it throws an error - in this case, the Error: `make` failed with exit code: 2.

$ npm install

# ...build output

gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:285:23)
gyp ERR! stack     at emitTwo (events.js:125:13)
gyp ERR! stack     at ChildProcess.emit (events.js:213:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:197:12)
gyp ERR! System Linux 4.5.12-ti-r64
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/projects/sa-web-cms/node_modules/bufferutil
gyp ERR! node -v v14.20.1
gyp ERR! node-gyp -v v3.7.4
gyp ERR! not ok

But specifically, what exactly does it mean that make failed with exit code 2?

When make fails with exit code: 2, it's usually because there was a problem with the build process. This could be due to a variety of reasons, like missing dependencies, syntax errors in the code, or even issues with the build environment.

Note: Exit codes are a way for programs to communicate what happened during their execution. An exit code of 0 usually means that the program completed successfully, while any other number indicates some sort of error.

How to Fix the Error

Fixing the gyp ERR! stack Error: `make` failed with exit code: 2 can be a bit tricky, as the actual problem might be hidden deep within the build process. However, here are a few steps that you can take:

  1. Reinstall the problematic package: One of the simplest things you can do is just to uninstall and reinstall the package causing the issue:
$ npm uninstall problematic-package
$ npm install problematic-package
Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

  1. Delete lock files: Another simple thing to try that, surprisingly, fixes a lot of errors is to delete your lock file and reinstall, which can be done with npm or yarn:
$ rm package-lock.json
# OR
$ rm yarn.lock
# THEN
$ npm install

By "cleaning" your environment and forcing a full install, you can sometimes help clear hard-to-find errors like this.

  1. Check your build environment: Make sure that you have all the necessary build tools installed. This includes make, a C++ compiler, and Python.
$ make --version
GNU Make 4.2.1
$ gcc --version
gcc (GCC) 10.2.0
$ python --version
Python 2.7.18
  1. Update your npm and node-gyp: Sometimes, the error might be due to a bug in npm or node-gyp. Updating these tools to their latest version can often fix the problem.
$ npm install -g npm
$ npm install -g node-gyp

Conclusion

In this Byte we attempted to explain what the error Error: `make` failed with exit code: 2 actually is, why it occurs, and a few ways to fix it. It's one of many potential errors that can seem hard to fix at first, but usually have a simple solution, like deleting a lock file and reinstalling.

Last Updated: October 4th, 2023
Was this helpful?

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms