How to crash my Node app on purpose?

Three things come to my mind:

  • You could just call process.exit. This for sure brings your application to a state where it needs to be restarted.
  • The other option might be to run an endless loop, something such as while (true) {}. This should make Node.js use 100% of your CPU, and hence the application should be restarted as well (although this, of course, means that you / someone has to watch your application).
  • Create a module in C that crashes by e.g. trying to access a random place in memory. I have no such module at hand, but I'm pretty sure that it should be quite easy for someone with C skills to write such a module.

I was attempting a similar thing with a /crash route in express, but just throwing an error from within the route handler was not enough to crash it.

process.exit would stop my app but forever would not restart it. (The forever logs just said something like process self terminated.)

What did work for me was inserting this into my /crash route:

setTimeout(function () {
      throw new Error('We crashed!!!!!');
}, 10);

Tags:

Node.Js