Reputation: 2570
I'm running a script in NodeJS and the script won't exit. I believe this is because of database connections that are initiated and haven't been closed. Is there a way to list the specific function that isn't letting node exit?
Upvotes: 1
Views: 86
Reputation: 27300
The why-is-node-running module seems to help here.
Install it temporarily, run your code through it, send it a SIGUSR1
signal when your program is stuck (the command is shown on stdout at launch) and then it will display everything that is still open.
In my case I wanted to run Jest tests through the module, and use ES6 modules, so the launch command was a bit more complex:
NODE_OPTIONS="--experimental-vm-modules" npx why-is-node-running ./node_modules/.bin/jest test/mytestfile.test.js
Normally you would just run
npx why-is-node-running myfile.js
For me, it told me that I had forgotten to close a database connection and that's why the code was lingering at exit.
Upvotes: 1