This error occurs when node-gyp cannot find a C++ compiler on your system. node-gyp is a tool used by Node.js packages to compile native C++ modules. It requires a C++ compiler (like g++ on Linux, clang on macOS, or Visual Studio on Windows) to be installed and properly configured.
node-gyp is a cross-platform command-line tool written in Node.js for compiling native C++ modules for Node.js. When a package requires native compilation (common for performance-critical modules like sqlite3, bcrypt, or sharp), npm runs node-gyp to build the C++ extension. This error means the system is missing the required C++ compiler tools. node-gyp attempts to detect available compilers during the build process, but if none are found (or they're not in the PATH), the build fails with this error. The error is not a problem with your Node.js installation, but rather that your system lacks the development tools needed to compile native modules. Different operating systems require different toolchains to be installed.
The first step is to install a C++ compiler appropriate for your OS:
On Ubuntu/Debian:
sudo apt-get update
sudo apt-get install build-essential python3On Fedora/RHEL:
sudo dnf install gcc gcc-c++ make python3On macOS:
xcode-select --installOn Windows, download Visual Studio Build Tools from https://visualstudio.microsoft.com/downloads/ and select "Desktop development with C++" workload.
Verify the compiler is available:
which g++node-gyp requires Python (version 2.7 or 3.5+) to generate the build files. Check that Python is installed:
python --version
python3 --versionIf Python is not found, install it:
On Ubuntu/Debian:
sudo apt-get install python3On macOS:
brew install python3After installation, verify it's in your PATH by restarting your terminal.
Sometimes npm cache can cause issues. Clear it and retry:
npm cache clean --force
npm installIf you're installing a specific package with native dependencies:
npm install <package-name> --build-from-sourceThis forces npm to rebuild the native module.
Verify that node-gyp is properly configured for your system:
npm config get python
npm config get cc
npm config get cxx
npm config get makeIf paths are missing or incorrect, set them manually:
npm config set python /usr/bin/python3
npm config set python /usr/bin/python3 --globalOn Windows with Visual Studio:
npm config set msvs_version 2019Incompatible Node.js versions can cause compiler detection issues. Check your current versions:
node --version
npm --versionUpdate to the latest LTS version. Using nvm (recommended):
nvm install node
nvm use nodeAfter updating, retry the installation:
npm installSome packages provide precompiled binaries for common systems, avoiding the need to compile locally:
npm install <package-name> --ignore-scriptsHowever, this only works if a prebuilt binary is available for your Node.js version and platform.
For some packages, you can specify prebuilt versions:
npm install sqlite3 --build-from-source=falseCheck the package documentation to see if prebuilt binaries are available.
Docker and Containers: When building Docker images, ensure the compiler toolchain is installed. In the Dockerfile, run: apt-get update && apt-get install -y build-essential python3
CI/CD Pipelines: Ensure your CI configuration installs build tools before running npm install. For GitHub Actions, add a step: sudo apt-get update && sudo apt-get install -y build-essential
Custom node-gyp Builds: If you're developing a native module, test the build manually with: node-gyp configure and node-gyp build. For detailed output, use: node-gyp build --verbose
WSL (Windows Subsystem for Linux): Install tools in the Linux environment, not Windows. Run: sudo apt-get install build-essential python3
Cross-Compilation: For advanced scenarios where you're compiling on one system for another, use the target-arch flag: npm install --target_arch=x64 --target_platform=linux
Error: EMFILE: too many open files, watch
EMFILE: fs.watch() limit exceeded
Error: Middleware next() called multiple times (next() invoked twice)
Express middleware next() called multiple times
Error: Worker failed to initialize (worker startup error)
Worker failed to initialize in Node.js
Error: EMFILE: too many open files, open 'file.txt'
EMFILE: too many open files
Error: cluster.fork() failed (cannot create child process)
cluster.fork() failed - Cannot create child process