Detecting CTRL+C in Node.js

If you’re trying to catch the interrupt signal SIGINT, you don’t need to read from the keyboard. The process object of nodejs exposes an interrupt event:

process.on('SIGINT', function() {
    console.log("Caught interrupt signal");

    if (i_should_exit)
        process.exit();
});

Edit: doesn’t work on Windows without a workaround. See here

Leave a Comment