为什么 - 是 - node - 运行
节点Js在跑,但你不知道为什么? why-is-node-running 是来帮你的.
安装
如果你想使用的话 why-is-node-running 在代码中,您可以安装它作为您的工程的本地依赖。 如果您想将其作为CLI, 您可以在全球安装, 或者使用 npx 运行它而不安装它.
作为当地依赖
20.11及以上节点(ECMAScript模块):
npm install --save-dev why-is-node-running
节点.js 8或以上(CommonJS):
npm install --save-dev [email protected]
作为全球一揽子计划
npm install --global why-is-node-running
why-is-node-running /path/to/some/file.js
或者,如果您不想在全球安装软件包,您可以用 npx数字 :
npx why-is-node-running /path/to/some/file.js
使用(作为依赖)
import whyIsNodeRunning from 'why-is-node-running' // should be your first import
import { createServer } from 'node:net'
function startServer () {
const server = createServer()
setInterval(() => {}, 1000)
server.listen(0)
}
startServer()
startServer()
// logs out active handles that are keeping node running
setImmediate(() => whyIsNodeRunning())
文件另存为 example.js,然后执行 :
node ./example.js
输出如下:
There are 4 handle(s) keeping the process running
# Timeout
example.js:6 - setInterval(() => {}, 1000)
example.js:10 - startServer()
# TCPSERVERWRAP
example.js:7 - server.listen(0)
example.js:10 - startServer()
# Timeout
example.js:6 - setInterval(() => {}, 1000)
example.js:11 - startServer()
# TCPSERVERWRAP
example.js:7 - server.listen(0)
example.js:11 - startServer()
使用率(作为CLI)
你们可以跑 why-is-node-running 如果你不想把它包含在代码中的话,作为一个独立。 发送 SIGUSR1编号 :SIGINFO 向进程发出信号将生成日志。 (Ctrl + T (关于macOS和BSD系统)
why-is-node-running /path/to/some/file.js
probing module /path/to/some/file.js
kill -SIGUSR1 31115 for logging
要触发日志 :
kill -SIGUSR1 31115
用法(与Node.js相通) --import 选项)
您也可以使用节点 --import 预装选项 why-is-node-running数字 :
node --import why-is-node-running/include /path/to/some/file.js
步骤与上述CLI部分相同
许可证
麻省理工学院