Skip to content
Latchkey

yarn "error Command failed with exit code 1" - Diagnose the Real Failure in CI

"Command failed with exit code 1" is yarn reporting that a script it ran exited non-zero. Like npm’s ELIFECYCLE, it is a wrapper - the real failure is in the lines above it.

What this error means

A yarn <script> step ends with error Command failed with exit code 1. The actual error - a failed test, a compiler error, a missing file - is printed just before the yarn summary, not in it.

yarn output
$ tsc -p tsconfig.json
src/index.ts:12:7 - error TS2322: Type 'string' is not assignable to type 'number'.

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation.

Diagnose it: the shell in CI is not your shell

Package scripts run under a different shell, a different PATH, and a non-interactive environment on a runner. Most scripts that fail only in CI are relying on something the login shell gave them locally: a tool on PATH, an environment variable from a dotfile, or a TTY.

Terminal
# what the script can actually see
npm run env | grep -E "^(PATH|NODE_ENV|CI)=" 

# is the binary on PATH for the script, not just for you?
npm exec -- which <tool> || echo "not resolvable from npm scripts"

# run the exact script with tracing
sh -x -c "$(node -p "require('./package.json').scripts.build")"

Common causes

The underlying command failed

The exit-code-1 message just reflects that the command your script ran returned non-zero - a failed build, test, or lint. yarn is the messenger.

How to fix it

Read above the yarn summary

  1. Scroll up from error Command failed with exit code 1 to the real error.
  2. Reproduce the underlying command directly (e.g. run tsc -p tsconfig.json) to iterate faster.
  3. Fix that error; the yarn wrapper message goes away with it.

Make failures fail the job

A multi-command script can report success while a middle command failed, which produces the worst kind of CI result: a green build that shipped something broken.

.github/workflows/ci.yml
# pipefail is NOT set by default in every runner shell
- name: Build
  shell: bash
  run: |
    set -euo pipefail
    npm run build | tee build.log

How to prevent it

  • Treat the message as "my script failed", not a yarn bug.
  • Surface the real command’s output clearly in CI logs.

Frequently asked questions

What causes yarn "error command failed with exit code 1"?
the underlying command failed. The exit-code-1 message just reflects that the command your script ran returned non-zero - a failed build, test, or lint.
How do I fix yarn "error command failed with exit code 1"?
Read above the yarn summary. Scroll up from error Command failed with exit code 1 to the real error.
What does yarn "error command failed with exit code 1" actually mean?
A yarn <script> step ends with error Command failed with exit code 1.
How do I stop yarn "error command failed with exit code 1" happening again?
Treat the message as "my script failed", not a yarn bug. The prevention section lists 2 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card