Skip to content

Instantly share code, notes, and snippets.

@msssk
Created May 27, 2020 05:29
Show Gist options
  • Save msssk/d336f9bfec996f15815180e873f02b99 to your computer and use it in GitHub Desktop.
Save msssk/d336f9bfec996f15815180e873f02b99 to your computer and use it in GitHub Desktop.
if (process.platform === 'win32') {
var readline = require('readline');
var readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
readlineInterface.on('SIGINT', function () {
process.emit('SIGINT');
});
}
@msssk
Copy link
Author

msssk commented May 27, 2020

If you have an event handler for "Ctrl+C" that only uses process.on('SIGINT') to gracefully terminate your Node.js process it does not quite work on Windows. As a Windows user I am quite accustomed to using development tools written in Node.js that I have to go through an additional step of confirming that I want to terminate the batch job:

Terminate batch job (Y/N)? y

If you include the code in this gist then Node.js processes on Windows will gracefully terminate from "Ctrl+C" with no additional confirmation prompt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment