Why does npm get killed?
npm is a tool that downloads code packages for Node.js projects. It can use a lot of memory. When the server runs out of memory (RAM), Linux protects itself and kills the biggest program. You see Killed.
Swap is a spare area on the disk that Linux uses like extra memory. It is slower than real RAM, but it stops crashes.
Step 1: Check current swap
- Log in to your server with SSH.
- Run this command:
free -h
It shows memory and swap in easy units. If the Swap line says 0, you have none.
Step 2: Create a swap file
Check you have free disk space first with df -h. These steps do not delete data, but a full disk causes other problems.
- Create a 2 GB file:
fallocate -l 2G /swapfile
This reserves 2 GB of disk space for swap.
- Lock the file so only root can read it:
chmod 600 /swapfile
- Format it as swap:
mkswap /swapfile
- Turn it on:
swapon /swapfile
- Run
free -hagain. The Swap line should now show 2.0G.
Step 3: Keep it after a reboot
- Back up the file first:
cp /etc/fstab /etc/fstab.bak
- Add the swap file to the list of disks:
echo '/swapfile none swap sw 0 0' >> /etc/fstab
This line tells Linux to turn swap on at every start.
Step 4: Run npm again
- Go to your project folder.
- Run
npm installagain.
dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. Swap is a safety net. If you often run out of memory, a bigger plan may help.Quick recap
- "Killed" means the server ran out of memory.
- Check swap with
free -h. - Create, lock, format and enable a swap file.
- Add it to
/etc/fstabto keep it after reboot.