Check Swap File to Prevent npm install From Being Killed

Check Swap File to Prevent npm install From Being Killed

Yesterday, I deployed my latest administration dashboard to staging environment. The dashboard uses AngularJS, NodeJS and Bootstrap. It can be built by using Grunt, Bower, Npm and tested using Karma and PhantomJS.

However, after I had transfered my source onto staging machine, I am unable to perform npm install. Command just freezes, and after 20 or 30 minutes, npm displays Killed message and breaks.

I had tried several ways:

  1. do npm install with every single dependency in package.json (about 20 dependencies, mostly grunt related)
  2. remove ^ and ~ character in package.json to ensure npm only fetches correct version of dependencies (no need to compare and get latest version)
  3. note that bower install can perform normally with about ~10 dependencies of AngularJS

try to npm rm -rf node_modules && npm cache clean, then npm install again does not work too, npm still be killed

Prevent npm install from being Killed

Hmm, then I search around Internet, and people rise this issue mostly by dirty workspace of node modules, and cleaning cache is enough. However, other people, like me, still faces this issue without luck. Because my local machine can run the commands without problem, and the remote machine has same environment, then the different is only about machine configuration.

  • My local machine: dual core 1.3Ghz, with 4GB RAM and 4GB swap file, 256GB SSD
  • My remote machine: single core CPU, with 512MB RAM and 20GB SSD
  • npm install was Killed, after laggy (|) rotation

So the biggest differ is memory, I can think my remote machine (which is running Apache, nginx and MySQL) has not enough memory for npm to build something while installing dependencies – several dependencies need to be rebuilt by make.

I go to check my swap file in remote machine:

sudo swapon -s

Then result is no swap file are available:

Filename Type Size Used Priority

I tried to create a 1GB swap file (on SSD system, swap speed can be much faster)

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k


sudo mkswap /swapfile

And activate:

sudo swapon /swapfile

Persist swap file after reboot by copying configuration on to

sudo nano /etc/fstab

/swapfile none swap sw 0 0

Make swap file to be a buffer to prevent application crashes:

echo 10 | sudo tee /proc/sys/vm/swappiness echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

Finally, set correct permission on it:

sudo chown root:root /swapfile sudo chmod 0600 /swapfile

Now, clean cache and rebuild with npm:

npm cache clean npm install

And voila! Install script works perfectly. It took about 5 minutes for npm and make to build and install needed dependencies. Then I can npm start and my dashboard is now online!

So, the conclusion is: when you already had success at something, then you fail with the same task, please look at what is different, including software and hardware to find out the reason of failure. Next time I use a VPS, I will make sure that swap file is available right after initial configuration.