I've got a simple nodejs program that tries to connect to NetworkManager via DBus using dbus-network-manager. Unfortunately, I can't use chrome://inspect to debug it because it behaves differently in the debugger.
Here's the code:
const NetWorkManager = require('dbus-network-manager').connect()
.then(nm => {
// this branch taken when run with or without --inspect option
console.log ('connected successfully');
})
.catch(err => {
// this branch taken when node launched with --inspect-brk option
console.log ('failed to connect, error was: ' + err);
})
And here's how it runs in 3 different scenarios:
$ node app.js
DBus test app running in development mode connected successfully
node --inspect app.js
Debugger listening on ws://127.0.0.1:9229/3fcf6bd4-f6fa-4bf2-8dcd-30c8fdcd14b8
For help see https://nodejs.org/en/docs/inspector
DBus test app running in development mode
connected successfully
node --inspect-brk app.js
Debugger listening on ws://127.0.0.1:9229/e935e8b4-0d78-4ad0-a5c1-894e5631c8fa
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
DBus test app running in development mode
failed to connect, error was: Error: No introspectable
I can work around the problem given that it seems to run ok with --inspect, but I'd love to know why this is happening at all.
Thanks!
Related
To debug a Node.js process, I use:
node --inspect-brk foo_bar.js
But what if that instance would start another, separate instance, and this one would be - how to run that instance in debug mode as well?
The problem is, I am using the commander.js library for Node.js, like so:
var program = require('commander')
...
program.parse(process.argv)
This creates another instance of Node.js process, and hence I lose the debug functionality (I am debugging via Chromium browser). How can I overcome this?
Note: Commander only creates another process if you implement the subcommand using a stand-alone executable.
Commander detects debugger options being passed to the node invocation, and passes them to the subprocess with the debugging port incremented by 1.
e.g.
// index.js
const { program } = require('commander');
program
.command('sub', 'stand-alone');
console.log('1');
console.log('2');
program.parse();
// index-sub.js
console.log('3');
console.log('4');
% node --inspect-brk index.js sub
Debugger listening on ws://127.0.0.1:9229/25005682-6bf4-44fb-bdb2-2cc7749bb328
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
1
2
Debugger listening on ws://127.0.0.1:9230/00d1924a-3122-4bef-86f2-65d562fbf3ed
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
3
4
I've started my application on my server with pm2:
pm2 start /path/lib/start-server.js --name="cdl-debug" -- --inspect
Which would be equivalent to node /path/lib/start-server.js --inspect
The application starts and runs, although I see no notice in the logs about any debugging like explained here
I've opened up port 9229 in the firewall and setup my WebStorm debug config with Attach to Node.js/Chrome like so:
Then when I run the debugger it tries to connect for a while and finally fails with the message: Connection timed out. No further information.
Is there something else I should do? The WebStorm documentation doesn't mention much about the required setup on the server.
When running node /path/lib/start-server.js --inspect, you are passing --inspect to your application, not to Node.js. As a result, debugger is not started. You need to make sure to pass --inspect-brk to Node.js in order to debug your app:
node --inspect-brk /path/lib/start-server.js
You can specify --inspect-brk in your pm2 process.json, like
"node_args": [
"--inspect-brk=7000"
]
and then start your app with pm2 start process.json
I am trying to learn to debug a Node.js code in WebStorm. The code is the simple "Getting Started" code from here:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
The above code works correctly after I ran node app.js at command line and visited http://localhost:3000. Next I tried to debug it locally in JetBrains WebStorm following instructions here. This is the debug configuration of Node.js application in WebStorm:
I clicked the bug icon to run the Node.js code under debugging mode. This is the output:
"C:\Program Files (x86)\JetBrains\WebStorm 10.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=24163 --nolazy --inspect app.js
Debugger listening on ws://127.0.0.1:24163/bffa51bd-74bd-4257-8174-2c8ab3768e17
For help see https://nodejs.org/en/docs/inspector
I guess that it is equivalent to running the "node app.js" command. The instruction says I need to "Perform the steps that will trigger the execution of the code with the breakpoints." I guess that means I open the URL "http://localhost:3000" in browser to send a request to the web server that the app.js is implementing, and I did it. I have set up breakpoints on every line of the source app.js and I expected that the breakpoints should be hit somewhere in the source, but the browser reported an "Unable to connect" error and nothing happened in WebStorm -- no breakpoint is hit. I have checked the instructions but could not figure out what I am missing. Could you please help me with this problem? If you need more information please let me know. Thanks a lot!
PS: app.js is in the ...\test folder.
==Edit:==
PS2: The browser can print "Hello world" just because "node app.js" is still running. After I terminated it, the browser reports "Unable to connect". So that means the URL does not know how to connect to the webserver launched by WebStorm debugger. Is the port wrong?
PS3: This is what returns from http://localhost:31496/json/list (the port number changes in every debugging session). Hope it can help troubleshooting.
description "node.js instance"
devtoolsFrontendUrl "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"
faviconUrl "https://nodejs.org/static/favicon.ico"
id "0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"
title "app.js"
type "node"
url "file://E:..._test_app.js"
webSocketDebuggerUrl "ws://127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"
As far as I can see, you are using webStorm 10. It's quite old and doesn't support new debugger protocol introduced in recent Node.js updates.
Please downgrade Node.js to v. 4.x (at least) or, better, upgrade Webstorm to 2017.3
How can I debug a running nodejs app? I've found tools such as node-inspector, but it seems to only support starting the app and debugging from there.
Debugging a running nodejs app.
This is the combination of a little documented feature of V8 mixed with a non-documented feature of the node.js debugger client. Say you have an already running node process that you want to debug.
# start the remote debugger in the already running node process
kill -s USR1 pid
# attach to the debugger using the node.js client
node debug host:5858
# see where you are
debug> pause
debug> bt
From there you can poke around. You can also continue and pause again to see if you seem to consistently end up in the same code area.
Debugging a nodejs app.
V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol. Node has a built-in client for this debugger. To use this, start Node with the debug argument; a prompt will appear:
% node debug myscript.js
< debugger listening on port 3000
connecting... ok
break in /home/username/Code/myscript.js:1
1 x = 5;
2 setTimeout(function () {
3 debugger;
debug>
cont, c - Continue execution
next, n - Step next
step, s - Step in
out, o - Step out
pause - Pause running code
Check API for other commands reference and other details
You can also use node-inspector . Use it from any browser supporting websockets. Breakpoints, profiler, livecoding etc... It is really awesome.
Install it with
npm install -g node-inspector
then run
node-debug app.js
I am trying to debug the child_process example from here using IntelliJ IDEA 12.1.3 and node 10.10. When I run nodejs app.js from a terminal everything works. The console output displays as expected. However, when I debug the same script using IDEA there are no messages in console output and the app just sits there. This is what is in the console window:
/usr/bin/nodejs --debug-brk=58954 app.js
debugger listening on port 58954
debugger listening on port 58954
When I run the script in IDEA without the debugger attached, the script works as expected.
Why does attaching the debugger break the script?
You can force the children to use a free port for debugging. InteliJ will automatically pick up the port chosen by the child process.
Here's an example:
// Determine if in debug mode.
// If so, pass in a debug-brk option manually, without specifying port.
var startOpts = {};
var isInDebugMode = typeof v8debug === 'object';
if(isInDebugMode) {
startOpts = {execArgv: ['--debug-brk']};
}
child_process.fork('./some_module.js', startArgs, startOpts);
looks like a bug in node.js fork to me: both parent and child processes receive --debug-brk=58954 switch and attempt to start debugger and listen port 58954.