Debugging Node, what supports it?

I have a script written for Node.Js that collects a load of data, processes it, saves and then finishes. Unfortunately, something I did was prevent the script from ending and closing the node, but it remains open.

I will probably just work on the changes I made and try to track them, but ... Is there an easy way to debug a node and find out what code / event / callback / connection or something else that it is waiting?

I looked at the node-inspector, but I couldn't figure out how to track anything opening it. Any tips?

+6
source share
3 answers

You can run your node.js program using wtfnode or add require('wtfnode').init(); to the first line or your program. It will then print a list of all things that support node when you press ctrl + c to kill the process.

+5
source

When this happened to me, most often something like mongodb, amqp, etc. These are database connections, network connections. Make sure you close them after processing is complete.

I think you could use lsof -p <PID> and see what that says. I can see both mongodb and amqp there.

+3
source

The moment you expect it to close, add

 console.log(process._getActiveHandles()); 

You can also add a handler.

 process.on('SIGINT', () => console.log(process._getActiveHandles()) ); 

So, if node will not close correctly, Ctrl + C will tell you why.

0
source

All Articles