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.

Introduction & Quick Summary
npm install or npm run build ends with a message that says nothing about the actual failure:
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.
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").
// 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:
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:
ls -t ~/.npm/_logs | head -3
grep -n "ELIFECYCLE\|errno\|command failed" ~/.npm/_logs/$(ls -t ~/.npm/_logs | head -1)Key Comparison Table
| Log clue | Meaning | Next step |
|---|---|---|
errno 1 right after ELIFECYCLE | generic script failure | read the script output above |
errno 127 | command not found | install the binary, check devDependencies |
errno 137 | killed by the OOM killer or CI timeout | raise memory, split the build |
npm ERR! missing script: build | no such script | check the package you are in |
Failed at the <pkg>@<version> <script> script | which script failed | run that script alone |
EBADENGINE above the summary | Node version outside engines | align Node versions |
| Script stage | Fails most often because | Typical fix |
|---|---|---|
preinstall / postinstall | native build tools, husky in CI | set HUSKY=0 in CI, install build tools |
prepare | no dev dependencies in production installs | run with --include=dev or guard the script |
prebuild / build | type errors, missing env vars | fix the error, validate env at boot |
test | failing test or no tests found | --passWithNoTests for empty suites |
postbuild | copying assets that were not produced | make the step tolerant or ordered |
| Environment | Common ELIFECYCLE source | Difference from local |
|---|---|---|
| Local dev | stale lockfile, wrong Node | npm may have resolved the script differently |
| CI | missing env var, different Node, memory cap | clean install, no cache |
| Docker | npm ci --omit=dev then build | dev dependencies absent |
| Windows vs Unix | shell syntax, rm -rf, path separators | use 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:
npm run build --foreground-scripts
npm run build --loglevel=verbose
npm_config_loglevel=silly npm run buildIn 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:
node -v
cat .nvmrc 2>/dev/null || cat .node-version 2>/dev/null
npm pkg get enginesPin 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:
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 outputValidate 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:
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:
NODE_OPTIONS=--max-old-space-size=4096 npm run buildIf 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_modulesas 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
|| trueto the script. The pipeline turns green while the artifact is broken or missing. - Using
--ignore-scriptseverywhere. It skipspostinstallfor native modules, which then fail at runtime with a missing binary. - Running from the wrong directory. In a workspace,
npm run buildat the root runs the root script; usenpm run build -w packages/app. npm civersusnpm install.npm cifails whenpackage-lock.jsonandpackage.jsondisagree, 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.
ELIFECYCLEidentifies 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 documentationThe Node.js documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Node.js work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Node.js guides
All Node.js guidesNode.js: ERR_DLOPEN_FAILED invalid ELF header
Invalid ELF header means a native addon was built for another OS or architecture. Rebuild it inside the target image instead of copying node_modules.
Node.js ERR_INVALID_ARG_TYPE: the argument must be of type
ERR_INVALID_ARG_TYPE is Node's validator naming a wrong argument. Read the quoted argument, validate at the boundary and default env values.
ERR_MODULE_NOT_FOUND: Cannot find module imported from an ESM file
ESM resolves specifiers literally: extensions are required, directories do not resolve to index files, and exports maps block deep imports.
Node.js 26 upgrade: rmdir, native addons and removed flags
Three breaks stop a deploy: rmdir with recursive throws, the transform-types flag is gone, and the module ABI moved to 147.
More in Node.js
All Node.js guidesThe guides filed next to this one in Node.js, in library order.