Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

yarn build is killed with exit code 137 in Bitbucket Pipelines

Errorerror Command failed with exit code 137.

Short answer

Exit code 137 means the build process was killed, and in Pipelines the usual reason is memory. A step has a fixed total: 4096 MB, or 8192 MB with size 2x, and services share it. Raising Node's heap above what the container has cannot help. Give the step size 2x, shrink the services, and keep the Node heap below the build container's memory.

  • Applies to Bitbucket Pipelines, Node.js 22, yarn 1 and 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at your bitbucket-pipelines.yml for three things: a size on the step, any services (including docker), and any NODE_OPTIONS or --max-old-space-size value. Those numbers decide how much memory the build has.

    grep -n -E "size:|memory:|services:|max-old-space-size|NODE_OPTIONS" bitbucket-pipelines.yml
  2. 2Match what you see, then jump to its fix

    1. 1exit code 137 during yarn build, no size line on the step.The step has the default 4096 MB total and the build needs moreFix
    2. 2The step has docker: true or services, and 137 appears although the total looks large enough.Services or the docker service take memory away from the build containerFix
    3. 3NODE_OPTIONS or --max-old-space-size is set to a number near or above the step's memory.The Node.js heap limit is set higher than the container can supplyFix

What does the error look like?

  • error Command failed with exit code 137.
  • exit code 137

Cause 1 of 3

The step has the default 4096 MB total and the build needs more

1Check

Regular (1x) steps have 4096 MB in total. A large (2x) step has 8192 MB.

grep -n -B2 -A6 "yarn build" bitbucket-pipelines.yml

2Fix

Add size 2x to the step that runs the build.

- step:
    name: Build
    size: 2x
    script:
      - yarn install --frozen-lockfile
      - yarn build

3Verify

Re-run the pipeline; the build step finishes without the 137 line.

# Bitbucket > Pipelines > the run > Build step log

Confirmed by support.atlassian.com

Did this fix it?

Cause 2 of 3

Services or the docker service take memory away from the build container

1Check

Each service container gets 1024 MB by default, and the Docker service does too. The build container gets what remains, with a minimum of 1024 MB. Pipes count as services.

grep -n -E "docker|services|pipe:" bitbucket-pipelines.yml

2Fix

Drop services the build step does not use. For one you need, set a smaller memory value in the definitions section.

definitions:
  services:
    docker:
      memory: 512

3Verify

Run the pipeline again; the build container now has more memory.

# Bitbucket > Pipelines > the run

Confirmed by support.atlassian.com

Did this fix it?

Cause 3 of 3

The Node.js heap limit is set higher than the container can supply

1Check

--max-old-space-size sets V8's old-space limit in MiB. Node's docs advise leaving room for other uses: on a 2 GiB machine they use 1536.

grep -rn "max-old-space-size" package.json bitbucket-pipelines.yml

2Fix

Set the heap below the build container's memory, and combine it with a 2x step if the build really needs more.

script:
  - NODE_OPTIONS=--max-old-space-size=6144 yarn build

3Verify

The build finishes, or fails with a Node heap error (which is a different, clearer problem) rather than being killed.

# Bitbucket > Pipelines > Build step log

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Give the build its own step with size 2x instead of sharing a step with tests and Docker.
  • Keep the Node heap value under the memory the build container really has.
  • Remove services from steps that do not need them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.