Enable/Add swap to Linux

  1. Create a 4 gigabyte swapfile
    sudo fallocate -l 4G /swapfile
  2. Secure the swapfile by restricting access to root
    sudo chmod 600 /swapfile
  3. Mark the file as a swap space
    sudo mkswap /swapfile
  4. Enable the swap
    sudo swapon /swapfile
  5. Persist swapfile over reboots (thanks for the tip, bman!)
    echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

Ref: StackOverflow: Node.js catch ENOMEM error thrown after spawn