How to fix npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version preinstall: `node script.js` npm ERR! Exit status 1 npm ERR! Failed at the package@version preinstall script in npm
Preinstall script failures block npm install before dependencies are even fetched. Common causes include permission issues, missing build tools, or memory constraints. Fix permissions or install required build dependencies.
Quick fix
Fixes ELIFECYCLE
EACCES: permission denied, mkdir '/path/to/dir'- Check the actual error message
- Fix npm permissions (recommended)
- Install build dependencies
- Clear cache and reinstall
EACCES: permission denied, mkdir '/path/to/dir'npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! package@version preinstall: `node script.js`npm ERR! Exit status 1npm ERR! Failed at the package@version preinstall script
What this error means
Preinstall scripts run before npm installs any dependencies. When a preinstall script fails, the entire installation is blocked at the very beginning. Preinstall scripts are typically used for: - Checking prerequisites (Node.js version, system requirements) - Setting up native module compilation - Running security checks or validations The most common causes are permission errors (the script can't create directories or files) and missing build tools (the script tries to compile native code).
Typical symptoms
- npm install fails immediately, before downloading packages
- Error shows 'preinstall' in the script name
- Permission denied errors in the output
- Works with sudo but fails without (not recommended)
- node-pre-gyp errors during preinstall
- Prisma or similar ORMs fail at preinstall step
Common causes
- Permission denied creating build directories
- Missing build tools (Python, make, g++)
- npm running with wrong user permissions
- Insufficient memory for compilation (exit code 137)
- Network issues downloading prebuilt binaries
- Corrupted npm cache
How to fix "npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version preinstall: `node script.js` npm ERR! Exit status 1 npm ERR! Failed at the package@version preinstall script"
The real error is above ELIFECYCLE. Look for:
EACCES: permission denied, mkdir '/path/to/dir'or
gyp ERR! find PythonThis tells you exactly what to fix.
Don't use sudo with npm. Instead, fix the directory ownership:
# Check npm prefix
npm config get prefix
# If it's /usr/local, change ownership
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}Or better, use nvm to avoid permission issues entirely.
If the error mentions gyp or native compilation:
macOS:
xcode-select --installUbuntu/Debian:
sudo apt-get install build-essential python3Windows:
npm install -g windows-build-toolsSometimes cached data causes preinstall failures:
npm cache clean --force
rm -rf node_modules package-lock.json
npm installIf you need to proceed urgently:
npm install --ignore-scriptsWarning: This skips all lifecycle scripts. Some packages won't work correctly without their scripts running. Only use for debugging.
Exit code 137 indicates out-of-memory:
# Increase Node.js memory limit
export NODE_OPTIONS="--max-old-space-size=4096"
npm installFor Docker, increase container memory allocation in docker-compose.yml or Docker Desktop settings.
Advanced notes
The --unsafe-perm flag allows npm to run scripts as root when installed with sudo, but this is a security risk:
# Not recommended for general use
npm install --unsafe-permOnly use this in controlled environments like CI/CD or Docker containers where you understand the implications.
For packages like Prisma that commonly have preinstall issues:
# Generate Prisma client manually after install
npm install --ignore-scripts
npx prisma generateIf you're using a CI system and preinstall fails with permission errors, ensure the CI user has write access to the workspace and npm directories.
Frequently asked questions
- What does "ELIFECYCLE" mean?
- Preinstall scripts run before npm installs any dependencies. When a preinstall script fails, the entire installation is blocked at the very beginning. js version, system requirements) - Setting up native module compilation - Running security checks or validations The most common causes are permission errors (the script can't create directories or files) and missing build tools (the script tries to compile native code).
- How do I fix "ELIFECYCLE"?
- Preinstall script failures block npm install before dependencies are even fetched. Common causes include permission issues, missing build tools, or memory constraints. Fix permissions or install required build dependencies.
- What causes "ELIFECYCLE"?
- Permission denied creating build directories Missing build tools (Python, make, g++) npm running with wrong user permissions
- Is the "ELIFECYCLE" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐npm Official Documentation
- ๐ฌStack Overflow: how to solve npm error npm err code elifecycle(Stack Overflow)
- ๐GitHub #17073: prisma/prisma(GitHub)
- ๐