yarn build is killed with exit code 137 in Bitbucket Pipelines
error 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.
Fix it fast
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.yml2Match what you see, then jump to its fix
- 1
exit 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
The 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
NODE_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
- 1
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.yml2Fix
Add size 2x to the step that runs the build.
- step:
name: Build
size: 2x
script:
- yarn install --frozen-lockfile
- yarn build3Verify
Re-run the pipeline; the build step finishes without the 137 line.
# Bitbucket > Pipelines > the run > Build step logConfirmed by support.atlassian.com
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.yml2Fix
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: 5123Verify
Run the pipeline again; the build container now has more memory.
# Bitbucket > Pipelines > the runConfirmed by support.atlassian.com
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.yml2Fix
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 build3Verify
The build finishes, or fails with a Node heap error (which is a different, clearer problem) rather than being killed.
# Bitbucket > Pipelines > Build step logConfirmed by nodejs.org
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
Servers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Azure App Service on Linux restarts the container and the site never starts
The container restarts again and again and the site returns an application error
App Service sends a warmup request to the container and restarts it if no HTTP answer comes back in time.
5 causes, each with a fixServers
bcrypt_lib.node: invalid ELF header inside a Docker container
Error: /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header
bcrypt is a compiled addon, so the file in node_modules only runs on the operating system it was installed on.
3 causes, each with a fixServers
AWS CDK cannot find module '@aws-cdk/aws-ec2' (or another @aws-cdk/aws-* package)
Cannot find module '@aws-cdk/aws-ec2'
CDK v2 no longer ships one npm package per service.
3 causes, each with a fixServers
cdk deploy fails in a GitLab job with spawnSync docker ENOENT
code: 'ENOENT'
ENOENT means the operating system found no docker program to run.
3 causes, each with a fixServers
A Google Cloud Function cannot reach Memorystore Redis and shows 127.0.0.1:6379
Error: connect ECONNREFUSED 127.0.0.1:6379
The address 127.0.0.1:6379 means the Redis client got no host and fell back to localhost, which inside a function is the function itself.
2 causes, each with a fix