Reputation: 53119
I have this problem very often with various Node.js scripts that I write. After everything is done, they do not exit. Usually, it's an unclosed socket or readline interface.
When the script gets bigger, this is really hard to find out. Is there a tool of some sort that would tell me what's NodeJS waiting for? I'm asking for a generic solution that would help debug all cases of NodeJS not exiting when it's supposed to.
Samples:
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (typeof process.stdin.setRawMode == "function")
process.stdin.setRawMode(true);
const keypressListener = (stream, key) => {
console.log(key);
process.stdin.removeListener("keypress", keypressListener);
}
process.stdout.write("Press any key...");
process.stdin.on("keypress", keypressListener);
readline
blocks Node if you forget to close the interfaceconst readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
setInterval(() => { }, 2000);
Upvotes: 5
Views: 1881