Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Node.js: npm ERR! code ELIFECYCLE

ELIFECYCLE is npm reporting that a package script exited non-zero. Read the log above the error, run the failing script directly, and fix the exit code.

~5 min readNode.js
#nodejs#npm#ci#build-errors
npm lifecycle script crate breaking at an exit code gate with a build log scroll beside it

Introduction & Quick Summary

npm install or npm run build ends with a message that says nothing about the actual failure:

code
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! my-app@0.1.0 build: `next build` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the my-app@0.1.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Quick answer: npm ran a package script, the script exited with a non-zero code, and npm labelled that exit ELIFECYCLE. The cause is in the lines above the summary — usually a compiler error, a failing test, a missing environment variable, or a Node version that cannot parse the source.

bash
npm run build --foreground-scripts # show the script's own output, not just the summary tail -n 60 ~/.npm/_logs/$(ls -t ~/.npm/_logs | head -1)

ELIFECYCLE is npm's term for "a script in the package lifecycle failed". It covers prebuild, build, postbuild, prepare, prepublishOnly, test, and the preinstall/install/postinstall chain. Because npm wraps every one of those with the same summary, the label is identical whether a TypeScript error, a Jest failure or a broken husky install caused it.

This guide explains how npm's lifecycle works, how to find the real error from the log, the six causes that produce this label most often, and why "just delete node_modules" usually does not fix it.

Core Mechanics, Signatures & Reproducible Setup

npm runs lifecycle scripts in order. For npm run build, npm executes prebuild if present, then build, then postbuild. For npm install, it runs preinstall, then installs dependencies, then install, then postinstall, and finally prepare for git dependencies and local packages.

The exit code of each script is propagated. A script that exits with anything other than 0 stops the chain, and npm prints its summary and exits with that code (or 1 when the script exits 127, "command not found").

jsonc
// package.json { "scripts": { "prebuild": "rimraf dist", "build": "tsc -p tsconfig.build.json && next build", "postbuild": "node scripts/copy-assets.mjs", "test": "vitest run" } }

A reproduction that shows the wrapper hiding the cause:

bash
mkdir elifecycle-lab && cd elifecycle-lab npm init -y node -e "const p=require('./package.json');p.scripts={build:'node -e \"console.error(\\'boom\\'); process.exit(2)\"'} ; require('fs').writeFileSync('package.json',JSON.stringify(p,null,2))" npm run build # npm ERR! code ELIFECYCLE npm ERR! errno 2 ...

The exit code in npm ERR! errno 2 is the script's own code, which is the first useful clue: 1 is a generic failure, 2 is often a CLI usage error, 127 means the command was not found, 134 is an abort, and 137 is the process being killed, usually for memory.

The log file is written for every run and keeps more than the terminal shows:

bash
ls -t ~/.npm/_logs | head -3 grep -n "ELIFECYCLE\|errno\|command failed" ~/.npm/_logs/$(ls -t ~/.npm/_logs | head -1)

Key Comparison Table

Log clueMeaningNext step
errno 1 right after ELIFECYCLEgeneric script failureread the script output above
errno 127command not foundinstall the binary, check devDependencies
errno 137killed by the OOM killer or CI timeoutraise memory, split the build
npm ERR! missing script: buildno such scriptcheck the package you are in
Failed at the <pkg>@<version> <script> scriptwhich script failedrun that script alone
EBADENGINE above the summaryNode version outside enginesalign Node versions
Script stageFails most often becauseTypical fix
preinstall / postinstallnative build tools, husky in CIset HUSKY=0 in CI, install build tools
prepareno dev dependencies in production installsrun with --include=dev or guard the script
prebuild / buildtype errors, missing env varsfix the error, validate env at boot
testfailing test or no tests found--passWithNoTests for empty suites
postbuildcopying assets that were not producedmake the step tolerant or ordered
EnvironmentCommon ELIFECYCLE sourceDifference from local
Local devstale lockfile, wrong Nodenpm may have resolved the script differently
CImissing env var, different Node, memory capclean install, no cache
Dockernpm ci --omit=dev then builddev dependencies absent
Windows vs Unixshell syntax, rm -rf, path separatorsuse rimraf/cross-env

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Get the script output back

npm hides script output when it buffers. Three ways to see it:

bash
npm run build --foreground-scripts npm run build --loglevel=verbose npm_config_loglevel=silly npm run build

In CI, --foreground-scripts also makes the failing command appear in the pipeline log at the moment it fails, which is where it is easiest to read.

2. Node version mismatch

A script that uses ??=, structuredClone, import.meta.dirname or a newer TypeScript target parses fine locally and fails in CI on Node 18:

bash
node -v cat .nvmrc 2>/dev/null || cat .node-version 2>/dev/null npm pkg get engines

Pin one source of truth and let CI read it. In Docker, use the same major image as local: FROM node:22-alpine with npm ci.

3. Environment variables that exist only on your machine

A build that reads process.env.STRIPE_KEY and renders a client from it throws inside the script, and npm reports only the exit code:

ts
const key = process.env.STRIPE_SECRET_KEY; if (!key) throw new Error("STRIPE_SECRET_KEY is not set"); // fails fast, in the script's own output

Validate configuration at the start of the build script rather than deep inside a page, so the message appears directly above npm's summary.

4. Test runners and empty suites

vitest run and jest exit 1 when no test files match, which surfaces as ELIFECYCLE on a fresh checkout:

{ "scripts": { "test": "vitest run --passWithNoTests" } }

The same happens when a test fails for an unrelated reason; the failing test name is in the output above the summary.

5. prepare and postinstall in CI

prepare runs after npm install in a local checkout, but not after npm ci --omit=dev. A prepare script that runs husky therefore fails in containers where there is no .git directory:

bash
HUSKY=0 npm ci # or in CI config env: HUSKY: "0"

Guard the script instead of relying on the environment: "prepare": "husky || true" is acceptable for optional tooling, never for a build step.

6. Memory and time limits in CI

Exit code 137 means the process was killed. Node's default heap on a small runner is often the limit:

bash
NODE_OPTIONS=--max-old-space-size=4096 npm run build

If the build still dies, split it (build the client and server separately) or raise the runner size; retrying the same command unchanged produces the same kill.

The JSON formatter is useful when a broken package.json — a trailing comma, a duplicated key — makes npm report a script error instead of a parse error, and the regular expression tester helps pull the failing script name out of a long CI log.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Deleting node_modules as the fix. A clean install fixes lockfile drift sometimes, but it cannot fix a type error, a bad env var or a failing test.
  • Adding || true to the script. The pipeline turns green while the artifact is broken or missing.
  • Using --ignore-scripts everywhere. It skips postinstall for native modules, which then fail at runtime with a missing binary.
  • Running from the wrong directory. In a workspace, npm run build at the root runs the root script; use npm run build -w packages/app.
  • npm ci versus npm install. npm ci fails when package-lock.json and package.json disagree, and its error appears as ELIFECYCLE over a lockfile message.
  • Abbreviated output on newer npm. npm 10 and 11 keep the summary short and push detail to the log file, so tail ~/.npm/_logs/* is often faster than scrolling.
  • Assuming the label names the culprit. ELIFECYCLE identifies the wrapper; the failing command and its exit code are the evidence.

Official documentation: npm scripts and lifecycle events and Node.js child process exit codes.

Official references

primary documentation

The Node.js documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Node.js work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Node.js guides

More in Node.js

All Node.js guides

The guides filed next to this one in Node.js, in library order.