How to fix npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version prepare: `npm run build` in npm
The prepare script ELIFECYCLE error occurs when a package's prepare hook (typically running npm run build) fails. Check build configuration and ensure all build dependencies are installed.
Quick fix
Fixes npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version prepare: `npm run build`
# Look for errors like:
error TS2307: Cannot find module...
ERROR in ./src/index.js
SyntaxError: Unexpected token...
# The prepare script runs 'npm run build', so the error
# is from your build tool (TypeScript, webpack, etc.)- Read the actual build error
- Clean install with cache clear
- Use npm ci for reproducible builds
- Skip Husky hooks in CI
# Look for errors like:error TS2307: Cannot find module...ERROR in ./src/index.jsSyntaxError: Unexpected token...# The prepare script runs 'npm run build', so the error# is from your build tool (TypeScript, webpack, etc.)npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! package@version prepare: `npm run build`
What this error means
The prepare script is a lifecycle hook that runs automatically after `npm install` completes. Many packages use it to compile TypeScript, build assets, or perform other setup tasks. When this script fails, npm reports an ELIFECYCLE error. This commonly occurs when installing packages from Git repositories (which need to be built after cloning) or when a package's build process has issues. The actual error is in the build command output above the ELIFECYCLE message.
Typical symptoms
- npm install fails after downloading packages during "prepare" phase
- Error mentions "prepare: npm run build" or similar build command
- Git-based dependencies fail to install
- Works when installing from npm registry but fails from GitHub
- CI/CD pipelines fail during dependency installation
- Husky or other git hooks fail during prepare
Common causes
- Build script (npm run build) fails due to errors
- Missing build dependencies (TypeScript, webpack, etc.)
- Node.js version incompatible with build process
- Insufficient memory for build (Docker/containers)
- Permission issues preventing file creation
- Husky git hooks failing in CI environments
- Missing environment variables required by build
How to fix "npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version prepare: `npm run build`"
The ELIFECYCLE message is a wrapper—find the real error above it:
# Look for errors like:
error TS2307: Cannot find module...
ERROR in ./src/index.js
SyntaxError: Unexpected token...
# The prepare script runs 'npm run build', so the error
# is from your build tool (TypeScript, webpack, etc.)Reset npm state completely:
# Clear npm cache
npm cache clean --force
# Remove node_modules and lock file
rm -rf node_modules package-lock.json
# Reinstall
npm installnpm ci is stricter and better for CI environments:
# Clean install from lock file
npm cinpm ci deletes node_modules first and installs exact versions from package-lock.json.
Husky git hooks often fail in CI where there's no .git directory:
# Disable Husky in CI
HUSKY=0 npm ci
# Or in GitHub Actions:
env:
HUSKY: 0Ensure Node version matches project requirements:
# Check for .nvmrc or engines in package.json
cat .nvmrc
cat package.json | grep -A 3 '"engines"'
# Switch versions with nvm
nvm use
# Or install required version
nvm install 18If running as root (common in Docker):
# Allow scripts to run as root
npm install --unsafe-perm
# Or set globally
npm config set unsafe-perm trueTo debug, skip lifecycle scripts:
# Install without running scripts
npm install --ignore-scripts
# Then manually run build to see errors
npm run buildAdvanced notes
The prepare script runs in these scenarios:
1. After npm install (without arguments)
2. After npm install <git-url> for git dependencies
3. Before npm publish
4. Before npm pack
For Git-based dependencies, the prepare script is essential because the published npm package contains built files, but a Git clone only contains source files.
If you're publishing a package, ensure your prepare script works correctly by testing with npm pack locally before publishing.
Frequently asked questions
- What does "npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version pre" mean?
- The prepare script is a lifecycle hook that runs automatically after npm install completes. Many packages use it to compile TypeScript, build assets, or perform other setup tasks. When this script fails, npm reports an ELIFECYCLE error.
- How do I fix "npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version pre"?
- The prepare script ELIFECYCLE error occurs when a package's prepare hook (typically running npm run build) fails. Check build configuration and ensure all build dependencies are installed.
- What causes "npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version pre"?
- Build script (npm run build) fails due to errors Missing build dependencies (TypeScript, webpack, etc.) Node.js version incompatible with build process
- Is the "npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! package@version pre" error serious?
- It is a medium-severity error — more of a blocker than a risk. 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 #7159: npm/cli(GitHub)