npm install error "npm ERR! code 1"
Problem
The "npm ERR! code 1" error is a generic error code that occurs during package installation with npm. In the specific case presented, this error occurs when trying to install Gulp.js, with the underlying issue being related to the node-sass
package.
The key error message in the log indicates:
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: C:\Program Files (x86)\nodejs\node.exe D:\www\wegrow\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose [...]
npm ERR! gyp verb `which` failed Error: not found: python2
The core problem is that node-sass
(which relies on node-gyp
) is trying to find Python 2, but many modern systems no longer have Python 2 installed by default.
Solutions
1. Replace node-sass with sass (Recommended)
The most effective long-term solution is to replace the deprecated node-sass
package with the modern sass
package:
npm remove node-sass
npm add sass
Alternatively, if you use Yarn:
yarn remove node-sass
yarn add sass
TIP
The sass
package is a pure JavaScript implementation of Sass that doesn't require native compilation or Python, making it compatible with all Node.js versions and operating systems.
2. Clear Cache and Reinstall Dependencies
If the error persists or is caused by other issues, try these steps:
# Clear npm cache
npm cache clean --force
# Remove existing dependencies
rm -rf node_modules package-lock.json
# Reinstall dependencies
npm install
For dependency conflicts, try:
npm install --legacy-peer-deps
3. Update Outdated Dependencies
If specific packages are causing the error, update them to their latest versions:
# Update all dependencies to latest versions
npx npm-check-updates -u
npm install
Alternatively, manually update the problematic package in your package.json
:
{
"dependencies": {
"node-sass": "^7.0.1" // Instead of older versions like ^4.14.1
}
}
4. Check Node.js and npm Compatibility
Ensure your Node.js and npm versions are compatible with your project:
# Check current versions
node --version
npm --version
# Update npm to latest version
npm install -g npm@latest
WARNING
Some older projects may require specific Node.js versions. If necessary, you can use a Node Version Manager (nvm) to switch between versions:
nvm install 14.16.1
nvm use 14.16.1
5. Install Build Tools (Linux/Ubuntu)
On Linux systems, ensure build tools are installed:
sudo apt-get update
sudo apt-get install build-essential
6. Identify the Problematic Package
If the error persists, systematically identify which package is causing the issue:
- Remove all dependencies from
package.json
- Add dependencies back one by one
- Run
npm install
after each addition - When the error reappears, you've found the problematic package
- Update or replace that specific package
Common Package-Specific Solutions
Some packages known to cause "npm ERR! code 1" issues:
- node-sass: Replace with
sass
package - sharp: Update to version ^0.30.0 or later
- libxmljs: Install specific version with
npm i libxmljs@1.0.11
- gulp-strip-debug: Downgrade to version 3.0.0
CI/CD Environments
For GitHub Actions or other CI/CD environments, ensure proper token configuration:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Alternative Package Managers
If npm continues to have issues, consider using Yarn:
npm install --global yarn
yarn install
Conclusion
The "npm ERR! code 1" error typically stems from compatibility issues between packages, Node.js versions, or missing system dependencies. The most effective approach is to:
- Replace deprecated packages like
node-sass
with modern alternatives - Ensure all dependencies are up-to-date
- Verify Node.js and npm version compatibility
- Clear cache and reinstall dependencies when necessary
By following these steps, you should be able to resolve the installation error and successfully install your project dependencies.