日本語

Fixing the npm “Lifecycle script failed” Error

​​npm​Node.js,npm workspaces,Troubleshooting,TypeScript

npm error Lifecycle script `build` failed with error: is a message that says the command defined in the scripts section of package.json (here, build) failed. When the command runs and fails, the real cause is shown above this message, as the command's own error. Fix that cause and this message goes away too.

I saw this error in a monorepo that uses npm workspaces, when running npm run build -w web or npm run build --workspaces. I reproduced it in a test environment, confirmed the output, and confirmed that fixing the cause makes the message disappear.

Note

The test environment was Ubuntu 24.04. I mainly tested with Node.js 22.23.4 (npm 10.9.9), and confirmed that Node.js 24.22.1 (npm 11.19.1) prints the same message. The build script used the tsc from TypeScript 7.0.2. I did not test Windows, macOS, yarn, pnpm, or other npm versions.

The Full Error Output

In the test, I wrote "workspaces": ["packages/*"] in the root package.json and placed packages such as packages/web. The build script in packages/web is just tsc.

json
{
  "name": "web",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "tsc"
  }
}

I added a deliberate type error to packages/web/src/index.ts.

ts
const count: number = "3";
console.log(count);

I ran npm run build -w web from the root folder. It stopped with the output below (exit code 2; the paths are placeholders).

text
$ npm run build -w web

> web@1.0.0 build
> tsc

src/index.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.
npm error Lifecycle script `build` failed with error:
npm error code 2
npm error path /path/to/mono/packages/web
npm error workspace web@1.0.0
npm error location /path/to/mono/packages/web
npm error command failed
npm error command sh -c tsc

With Node.js 24.22.1 (npm 11.19.1), the output was exactly the same. The script name is wrapped in backticks, not in double quotes like "build".

The Cause Is Printed Above the npm error Lines

Lines that start with npm error are npm's summary that the command failed. They do not say what went wrong. The line to look at is the one above them.

text
src/index.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.

This error comes from tsc, not from npm. It means that on line 1 of src/index.ts, a string is assigned to a variable typed as number.

The npm error lines can be read like this:

Line Meaning
code 2 Exit code of the failed command. Here it is the 2 returned by tsc.
path / location The folder where the command ran.
workspace web@1.0.0 The name and version of the workspace (package) that failed.
command failed A line stating that the command failed.
command sh -c tsc The command that was actually run. It shows the contents of scripts as-is.

code is not a number npm defines itself; it is the exit code of the command. The value of code depends on the tool. When I ran "bundle": "vite build" without vite installed, sh: 1: vite: not found appeared above, and the output ended with npm error code 127. The shell (sh) could not find vite and exited with 127, and that value is shown as code. For the vite: not found error, see the separate article "How to Fix the vite: not found Error in npm run dev." In that article I confirmed that running npm install clears it.

Fixing the Type Error Makes It Go Away

I changed "3" to 3 in packages/web/src/index.ts.

ts
const count: number = 3;
console.log(count);

When I ran npm run build -w web again, it finished without any npm error lines (exit code 0).

text
$ npm run build -w web

> web@1.0.0 build
> tsc

Running the generated packages/web/dist/index.js with node printed 3. npm run build --workspaces also finished without errors.

This Message Appears When Running in a Workspace

I created a separate project that does not use workspaces, with the same tsc setup and the same type error, and ran npm run build on it. No npm error lines appeared. The output ended with the single line from tsc. The exit code was also 2.

text
$ npm run build

> single@1.0.0 build
> tsc

src/index.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.

In the npm 10.9.9 source, the Lifecycle script `…` failed with error: statement appears only in the code that runs scripts per workspace (execWorkspaces in run-script.js). When workspaces are not used, the code that builds the error message treats the error as coming from the command itself. It does not print npm's message and only passes through the exit code.

This message can also appear without -w. In my tests, it appeared in these two cases:

  • When I ran npm run build inside the packages/web folder
  • When the root scripts had "build": "npm run build --workspaces" and I ran npm run build at the root

The first case follows the comment in npm's startup code (npm.js), which says that if the current folder is a workspace, that workspace is the target. Even if you did not type -w, if you see this message, a package inside the monorepo is the one being run.

With –workspaces, npm Continues to the Next Package After a Failure

I created three packages, api, web, and worker under packages, and ran npm run build --workspaces with a type error only in web. After api, web failed, and npm still ran the build for worker.

text
$ npm run build --workspaces

> api@1.0.0 build
> tsc


> web@1.0.0 build
> tsc

src/index.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.
npm error Lifecycle script `build` failed with error:
npm error code 2
npm error path /path/to/mono/packages/web
npm error workspace web@1.0.0
npm error location /path/to/mono/packages/web
npm error command failed
npm error command sh -c tsc


> worker@1.0.0 build
> tsc

worker succeeded, but the exit code of the whole npm run build --workspaces run was 2. When the output is long, the error can get buried in the middle and be easy to miss. Search for npm error Lifecycle script, then check the line above it and the workspace line. Those show which package failed and what went wrong.

The Lifecycle script Line Also Appears for a Name Not in scripts

When I used -w to run a name that is not in that package's scripts, the first line was still Lifecycle script. In this case there are no code or command lines, and the cause, Missing script, follows below (the paths are placeholders).

text
$ npm run lint -w web
npm error Lifecycle script `lint` failed with error:
npm error workspace web@1.0.0
npm error location /path/to/mono/packages/web
npm error Missing script: "lint"
npm error
npm error Did you mean this?
npm error   npm link # Symlink a package folder
npm error
npm error To see a list of scripts, run:
npm error   npm run --workspace=web@1.0.0

For how to handle the Missing script message itself, see the separate article "How to Fix the Missing script Error in npm run."

If you want to run a script that only some packages define across all workspaces with --workspaces, add --if-present, as described in the workspaces documentation. In my test, when no package had a lint script, npm run lint --workspaces --if-present finished without errors (exit code 0).

​Posted by 86