Scroll back to the top

Non-blocking node.js exit on key press

[ nodejs console javascript ]

From time to time it needs to implement some CLI tool. There is the approach to wait for any key press, then exit the process. The feature doesn’t block your process. So, there is some listener or other useful logic can still be in progress.

process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));

Happy coding!