Skip to main content

npm install Killed (Exit Code 137): 4 Easy Fixes

3D illustration of a server stack with glowing memory chips flying out, a terminal showing npm install Killed and a large 137 exit code

Seeing npm install killed with exit code 137 usually means the Linux out-of-memory killer stopped it. Exit code 137 is 128 + 9: the process received SIGKILL. The same cause applies when pnpm install dies on a small machine.

This is common on 1 GB servers, such as a Google Cloud e2-micro. Confirm the cause first, then pick the smallest fix that works.

npm install killed: how the Linux OOM killer ends the process with exit code 137
How npm install ends up killed with exit code 137 on a small VM.

Why npm install killed errors happen on small VMs

npm and pnpm unpack many packages and may run build scripts. On a machine with 1 GB of RAM and no swap, one heavy script can use all the memory. When RAM and swap are both full, the kernel’s out-of-memory (OOM) killer ends the largest process to keep the system alive.

Your terminal shows only the word Killed, with no stack trace. Because any SIGKILL gives exit code 137, check the kernel log before you change anything.

1. Confirm the OOM killer is why npm install killed your process

Look for a line in the kernel log that names the killed process. The dmesg manual page lists the options.

free -h
sudo dmesg -T | grep -i -E "out of memory|killed process"
sudo journalctl -k | grep -i -E "out of memory|killed process"

If the log shows node, npm or pnpm being killed, memory is the problem. If it shows nothing, something else sent the signal, for example a timeout or a manual kill.

2. Fix: add swap

Swap is the fastest fix on a machine with no swap at all. This creates a 2 GB swap file and keeps it after a reboot.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

With swap in place, npm install killed errors usually stop, although the install runs slower. Swap on a small cloud disk is slow, so it prevents the kill but is not a replacement for enough memory.

3. Find out which step uses the memory

Run the install without lifecycle scripts. If it succeeds, the spike comes from a post-install or build script rather than from downloading packages. The pnpm install documentation describes the option.

npm install --ignore-scripts
pnpm install --ignore-scripts

When npm install killed the process only during one script, that script is the one to fix. Run it on its own, with the options below or on a larger machine.

4. Limit Node’s memory use

Setting a heap limit below what the machine can spare makes Node fail with a clear out-of-memory error instead of being killed silently. Pick a value that leaves room for the operating system. The Node.js command-line options page explains both the variable and the flag.

export NODE_OPTIONS=--max-old-space-size=512

5. Don’t build on the small machine

Build in CI or on a larger instance and deploy only the output. Moving up one instance size is often cheaper than the time spent fighting a 1 GB machine.

Which fix to try first

Work through the fixes in the order below and stop at the first one that works. Confirming the cause is free, swap and skipping scripts take minutes, and a bigger machine is the last resort.

npm install killed: which fix to try first, from checking dmesg to using a larger machine
Try the fixes in this order and stop at the first one that works.

Checklist when npm install killed keeps happening

  • Confirm the kill in the kernel log with dmesg or journalctl.
  • Add a 2 GB swap file if the machine has none.
  • Run the install with --ignore-scripts to find the heavy step.
  • Set a Node heap limit so failures are clear instead of silent.
  • Build elsewhere, or move to a larger instance.

Docker builds and Kubernetes jobs

If the install runs inside docker build or a Kubernetes job, the limit may be the container’s memory limit rather than the VM’s. For the Kubernetes case, see Kubernetes OOMKilled: how to fix exit code 137.

Frequently asked questions

Does exit code 137 always mean out of memory?

No. Any SIGKILL gives 137, including kill -9. Check the kernel log to be sure.

Why was npm install killed with no error message?

The kernel ends the process with SIGKILL, which cannot be caught, so npm has no chance to print an error. The only trace is in the kernel log.

Will more swap fix it?

It usually stops the kill, but the install gets slower. If the machine is regularly short of memory, use a larger instance.

Is this specific to Debian or Ubuntu?

No. The OOM killer is part of the Linux kernel, so the same cause and checks apply on other distributions.

Summary

In short, npm install killed with exit code 137 is almost always a memory problem on a small machine. Confirm it in the kernel log, add swap, find the heavy script, and cap Node’s memory. If the machine is still too small, build elsewhere.

Related reading

A practical guide for your next challenge. A fresh perspective for everyday life. Discover both through Nandan’s latest articles.

Post Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsAppLet's connect