Switching NodeJs application to debug mode from within process
I have a NodeJs "some-module" module that I want to install globally, so it can be run directly from the command line without the node executable prefix. ie: $> some-module [args]
I would like one of these arguments to be --debug
. The reason for this is that I don't want users of this module to install it in their local directory in order to run node --debug-brk node_modules/some-module/[path to entry point] [args]
.
NodeJs documentation has an advanced debugging section ( http://nodemanual.org/latest/nodejs_ref_guide/debugging.node.js.html )
The V8 debugger can be enabled and accessed either by running Node.js with a command line flag
--debug
, or by signaling an existing Node.js process withSIGUSR1
.
I tried to do it with
process.kill(process.pid, 'SIGUSR1');
What caused the error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Unknown signal: SIGUSR1
at EventEmitter.kill (node.js:366:17)
at Object.<anonymous> (c:\dev\some-module\app.js:94:17)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
What do I need to do to get the running process into debug mode?
Also, I would like to debug the given application using node-inspector.
source to share
I'm not really sure if I understood your question, but ...
Maybe you can install the app globally and stop it at a breakpoint using npm. In package.json
put:
...
"scripts": {"start": "node --debug-brk some-module.js"},
"bin" : { "some-module" : "./some-module.js" },
...
The launch npm start -g some-module
will be split on the first line. Then you can use node-inspector for debugging.
About the stop part inside the code node has a built-in debugger (which is pretty rudimentary), but it allows this functionality. If you included somewhere in your code:
debugger;
and run:
node debug some-module.js
it will stop in the debugger (note: this is not the same as node-inspector, I don't know if this can be achieved using node-inspector).
I don't understand why you are doing this, but hope it helps.
source to share