Npm install fails to install dependencies from package.json. Encountering gyp ERR! issue

I’m using Ubuntu 20.04, with Node version 16.13 and NPM version 8.3. When I execute npm install based on the following package.json dependencies, I encounter an error:

  "dependencies": {
    "http-request": "^1.0.0",
    "form-parser": "^1.1.0",
    "access-control": "^2.0.0",
    "config-loader": "^1.0.0",
    "server": "^5.0.0",
    "module-bundler": "^3.0.0",
    "cli-tool": "^1.1.0"
  },
  "devDependencies": {
    "babel-runtime": "^7.0.0",
    "webpack-bundle": "^2.0.0",
    "test-framework": "^1.0.0"
  }
}

I'm receiving this error message:

npm ERR! code 1
npm ERR! path /path/to/project/node_modules/node-sass
npm ERR! command failed
npm ERR! command sh -c node scripts/build.js
npm ERR! Building: /usr/bin/node /path/to/project/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb node-gyp version: 3.8.0
npm ERR! gyp verb Node.js version: v16.13.0
npm ERR! gyp verb command rebuild []
...

I'm puzzled by the following questions:

  1. Why do I see two different versions of node-gyp being referenced?
  2. Why aren't there any files in the node_modules/ directory?
  3. What steps can I take to resolve this and successfully run npm install?

Here's how to resolve the issue:

  1. Node & NPM Compatibility: Ensure node & npm versions are compatible. Update both using:

sudo npm install -g n && sudo n stable; npm install -g npm@latest
  1. Rebuild Node-Sass: If node-sass is indirectly required, rebuild it:

npm rebuild node-sass
  1. Clean Cache & Reinstall: Clear npm cache and reinstall dependencies:

npm cache clean --force; rm -rf node_modules package-lock.json; npm install
  1. Verify Build Tools: Ensure build-essential and Python are installed:

sudo apt-get install build-essential python3

Try these steps to address the issues and run npm install successfully.

Hello DancingButterfly,

Resolving a gyp ERR issue usually involves checking the setup and dependencies. Let's resolve your issue step-by-step:

  1. Check Node & npm Versions: Ensure compatibility by updating Node.js and npm. Run the following commands:

    sudo npm install -g n && sudo n stable
    npm install -g npm@latest
  2. Address Node-Sass Error: It seems like node-sass is causing a problem. Rebuilding it might help:

    npm rebuild node-sass
  3. Clear Cache and Retry: Sometimes, clearing the npm cache and reinstalling can fix various issues:

    npm cache clean --force
    rm -rf node_modules package-lock.json
    npm install
  4. Install Required Build Tools: Make sure you have all necessary development tools installed:

    sudo apt-get install build-essential python3

These steps should help you resolve the errors and perform a successful npm install. Always ensure your development environment is configured correctly to avoid such issues in the future.

To address the gyp errors during npm install on Ubuntu 20.04, the following steps could provide a different perspective:

  1. Verify Node Version Requirements: Some packages may have specific Node.js version requirements. Check if any of your dependencies require a Node version other than 16.13. You can do this via their respective documentation or repositories.
  2. Check Node-Sass Compatibility: Ensure that node-sass is compatible with your Node version. If node-sass doesn't support Node 16, consider using dart-sass as a replacement:

    npm uninstall node-sass
    npm install sass
  3. Remove Pre-build Binaries: If previous binary versions are stuck, it may help to clear them out:

    rm -rf ~/.node-gyp
  4. Check for Multiple Node-Gyp Installations: Sometimes conflicts arise from multiple installations of node-gyp. You can list globally installed packages and remove duplicates:

    npm list -g --depth=0

    Remove any unnecessary or conflicting versions with:

    npm uninstall -g 
  5. Use Node Version Manager (NVM): To manage Node.js installations more flexibly, consider using NVM. This allows for easy switching between Node versions:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    nvm install 16.13
    nvm use 16.13

Implementing these steps should help you circumvent persistent installation issues. Always ensure you're using package versions compatible with your Node and npm setup to minimize errors.