Categories
JavaScript Answers

How to detect CTRL+C in Node.js?

Spread the love

To detect CTRL+C in Node.js, we can listen for the SIGINT event.

For instance, we write

process.on("SIGINT", () => {
  console.log("Caught interrupt signal");

  if (shouldExit) {
    process.exit();
  }
});

to listen for the SIGIN event with process.on.

We call it with a callback that’s called when ctrl+c is pressed.

In it, we call process.exit to exit the program if shouldExit is true.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *