Debugging “Make Failed with Exit Code 2” Error

 A “make” error occurs when there is a problem executing the “make” command while developing a Node.js application. The error message itself will provide information about the problem that caused it to occur.

Generally, the “make failed with exit code 2” error occurs during dependency installation.

If you encounter a “make failed with exit code 2” error during the installation of a NodeJS module, it usually indicates a problem with the build process of any module you tried to install. 

The stack trace will provide more information about the cause of the failure. Which is shown below.

Stack Trace:

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.4.30-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/WibiSmart-Bluemix-App/node_modules/bufferutil
gyp ERR! node -v v8.0.0
gyp ERR! node-gyp -v v3.6.1
gyp ERR! not ok

The reason behind this npm err is not singular. Make sure you have the latest Node and npm installed in your system. Also, make sure the build-essentials package is installed in your system.

Let’s try to debug and fix it.

Steps to Fix “make failed with exit code 2” Error

Below are some steps you can follow to debug the error.

1. Deleting the package-lock.json file

Try to remove the package-lock.json file(not package.json) and the node_modules folder. Then re-install the modules.

Below is the command to remove the package-lock.json file and node_modules folder:

rm -rf package-lock.json
rm -rf node_modules

Then re-install the modules:

npm install

This should fix the error. If not, let’s move on to the next error.

2. Deleting the .node-gyp folder

Delete the .node-gyp folder that should be present in your home directory.

Below is the command to remove the .node-gyp folder:

rm -rf ~/.node-gyp

Then re-install the modules:

npm install

3. Re-install node-gyp

Try re-installing node-gyp again.

Below is the command to install the node-gyp:

npm i -g node-gyp

You may require sudo access depending on your node installation.

4. Update Node and NPM

The old Node version or NPM can also cause this issue. Re-install it with build modules and check if it works. You can also re-install node modules using NPM.

5. Install Build Essentials

Try installing/re-installing build-essentials packages.

Below is the command to install build-essentials for the Ubuntu operating system:

sudo apt-get install -y build-essential

After executing this command, you will be asked to enter your password, after which the package manager will download and install the build-essential package.

Frequently Asked Questions (FAQs)

What is exit code 2 error?

Exit code 2 is a generic error code that indicates that a process failed to complete its operation successfully. In Node.js it generally occurs during the installation of modules.

What is make command failed with exit code 2?

The “make” command fails with exit code 2 indicating that there is a problem with the build process executed by the “make” command.

How do I know if node-gyp is installed?

To check that node-gyp is installed on your system, you need to execute the command “node-gyp -v”. You will get the version number of node-gyp if it is installed, otherwise, you will get an error that this command is not found.

Do I need to install node-gyp?

No, it is not necessary to install node-gyp in every node project. You only need to install if a node dependency requires a native add-on that needs to be built during installation. node-gyp is basically used when we are dealing with dependencies that have native code components written in C/C++.

Conclusion

If these problems are still here, please make sure you are not using deprecated modules. Also, make sure you have installed npm properly, maybe there is a problem with npm. Also if the Node.js version is too old then it can also create problems. If you are still facing the issue, check out the StackOverflow link below for more discussion on this.

Reference

https://stackoverflow.com/questions/44316064/gyp-err-build-error-stack-error-make-failed-with-exit-code-2

Pankaj Kumar
Pankaj Kumar
Articles: 207