debugging node.js child_process fork example on IntelliJ IDEA - node.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.

Related

Retain Node.js debug mode when starting another process

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

NodeJS debug Starting inspector on 127.0.0.1:42457 failed: address already in use error

I am trying to debug my Discord bot using the DiscordJS lib. My trouble is that when I start a debug session (launching with vscode or with --inspect-brk | --debug-brk)
I get a Starting inspector on 127.0.0.1:42457 failed: address already in use error I have tried changing the port in both situations and still getting the same error on different ports. My bot is sharded so its start child process of itself. I have tried using vscode to attach to child processes but still the same error.
If anybody is familiar with DiscordJS and knows how to debug a sharded bot some help would be much appreciated
In my case to solve this, just add that config on nodemon.json
{
"execMap": {
"js": "node --inspect=9300 -r sucrase/register"
}
}
I added --inspect=9300 to set port to this specific port
First Ctrl C (or Cmd C) to quit all your progress in the cmd.
Start Task Manager > Find all tasks named "node" > End process.
Now go back to your cmd and start server.
That is because you are already running that application in with node filename.js, hence stop that process and start with debugging again with same port.
You can take a look that on the below video might help
https://www.youtube.com/watch?v=b8e9RAekktY&t=28s

Debug pipelined Node.js processes

I have a command that looks basically like so:
node --inspect-brk=9229 a.js | node --inspect-brk=54031 b.js
When I start this command at the command line, I get this:
Debugger listening on ws://127.0.0.1:54031/66c60348-ce22-4acc-9ba3-aa97b8dd1f12
For help see https://nodejs.org/en/docs/inspector
Debugger listening on ws://127.0.0.1:9229/59c70b31-8af6-4c99-bdaf-c1a86f49d62b
For help see https://nodejs.org/en/docs/inspector
They are hardcoded to listen on different ports, one on the default (9229) the other on 54031.
However, when I debug with Chrome tools, only one Chrome debug window opens and when I try different urls, I cannot get a second debugging session to open.
The url that works is like so:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=localhost:9229/59c70b31-8af6-4c99-bdaf-c1a86f49d62b
and if I try pasting this into a new window:
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws://127.0.0.1:54031/66c60348-ce22-4acc-9ba3-aa97b8dd1f12
it doesn't work
Change
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws://127.0.0.1:54031/66c60348-ce22-4acc-9ba3-aa97b8dd1f12
to
chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:54031/66c60348-ce22-4acc-9ba3-aa97b8dd1f12
true&ws:// to true&ws=
I too tried this and its working just fine.

How can I use readline-sync npm repository with WebStorm?

I wrote the following code using 'readline-sync' dependency.
var readlineSync = require('readline-sync');
function main() {
printMenu();
var userName = readlineSync.question('Please enter your choice:');
console.log(userName);
}
main();
I ran this code from WebStorm trying to use the WebStorm console window.
I got the error:
Error: The current environment doesn't support interactive reading
from TTY. stty: when specifying an output style, modes may not be set
When I run it from linux terminal the code works with no error. I understand from the error message that 'readline-sync' cannot work from WebStorm console. Do you have any idea how to solve it?
I found out the answer.
type in WebStorm terminal:
$ node --debug-brk
Web storm will give you the port number the debugger is listening to. On my machine it was 5858. Then press 'Ctrl+C'.
Create a new debugging configuration as the following one:
In WebStorm terminal type again: "$ node --debug-brk main.js "
put a breakpoint somewhere.
Click the debugging icon
Happy Debugging!

nodejs start debugger on different port

Im a little confused with all this nodejs debug syntax flying around.
I simply want to start the debugger on a process when I run it on a different port.
Normally I start debugging by node debug file.js
but now I have to process` running that I need to debug
Now I found the command node --debugger=7873 file.js but that starts the debugger and jumps past all the breaks and I tried node --debugger=7837 --debug-brk file.js but that forces me to consume another teminal window. How can I just run a script on a different port in the same terminal or with out using nohup?
node debug --port=[your port] your_program.js
responsible _debugger code here

Resources