我编写的各种Node.js脚本经常遇到这个问题。完成所有操作后,它们不会退出。通常,它是未封闭的套接字或readline接口。

当脚本变大时,这真的很难找到。是否有某种工具可以告诉我NodeJS在等待什么?我正在寻求一个通用的解决方案,该解决方案将有助于调试NodeJS在应有的情况下不退出的所有情况。

样品:

图表I-即使删除了侦听器,进程stdin也会阻塞节点

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如果您忘记关闭界面,则会阻止节点

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});


图表III。 -被遗忘的setInterval也会阻塞节点,祝你好运找到它

setInterval(() => { }, 2000);

最佳答案

why-is-node-running可以工作吗?似乎完全可以满足您的需求。

关于javascript - 如何找出导致Node.js退出的原因?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50812262/

10-12 17:43
查看更多