WebStorm debugger does not attach on first time - node.js

When debugging a Node.js script with WebStorm, this is what my script looks like (windows):
"debug": "npm run build && node %NODE_DEBUG_OPTION% ./dist/server.js"
In the console I can see that npm run build executes as usually with success. Then there is a red message that states the following
Debugger listening on ws://127.0.0.1:51311/0064d764-16b8-4b45-bdff-5a1732e9ee62
For help see https://nodejs.org/en/docs/inspector
I can wait as long as I want - WebStorm won't attach and the ./dist/server.js script is never executed. When I stop execution and re-run the script, it succeeds and the debugger attaches.

Related

VSCode's Auto Attach is not toggling for Node.js

I use WSL to start VSCode with code . from my project directory in Windows Terminal. When VSCode starts up, "Auto Attach: Off" is shown in the status bar:
Clicking "Auto Attach: Off" in the status bar does not toggle it.
I then start my project in VSCode's integrated terminal with npm run debug, which corresponds to the following scripts line in package.json:
"scripts": {
"debug": "NODE_ENV=development DEBUG='knex:*' nodemon --inspect ./server.js"
}
However, the debugger is not auto-attached, so I need to manually run the Attach configuration from the Run panel shown on the left here:
The debugger works, but I still cannot toggle the Auto Attach: Off to be On instead:
Even if I Ctrl+Shift+P --> Debug: Toggle Auto Attach, nothing happens. Even if I restart VSCode after doing so.
So, in conclusion, how can I start my VSCode with code . from Windows Terminal and then just type npm run debug and have VSCode, run my program AND auto-attach the debugger? I also need the server to restart upon file changes, which is why I am using nodedemon in the npm run debug script.
Do I need to either:
change launch.json configuration? Though I have tried essentially all of the boilerplate launch.json configs shown in VSCode, but none worked.
or modify my package.json defined script for debug?
or correct some setting on VSCode?

node dist/server.js exits without error messages

I have an Angular Universal app.
I am able to run 'npm run start', which executes 'ng serve' and the live development server starts listening.
If instead I run 'node dist/server.js', it simply exits without any info or error messages.
check out the $.scripts.start field within the project's package.json file. That will tell you what command is being invoked through npm start, which might not necessarily be node dist/server.js. Default values for the start script are found here
Also, depending on what flavor of Javascript they are running, there might be a build step that you are missing.

Protractor Debugging Issue - Waiting for the debugger to disconnect

I am trying to debug protractor scripts on VSCode.
I edited the launch.json file but the debug console throws the below error.
Here is the VSCode Debug Console Output:
C:\Program Files\nodejs\node.exe --inspect-brk=45448 conf.js C:\Users\abc\AppData\Roaming\npm\node_modules\protractor\example/conf.js
Debugger listening on ws://127.0.0.1:45448/ab61a2d7-4b40-456b-86cb-838e94dfe5b0
Debugger attached.
Waiting for the debugger to disconnect...
I am not able to find a solution anywhere online either.
When you set your breakpoint in VSCode, you'll need to run the following:
node --inspect-brk ./node_modules/.bin/protractor conf.js
The path above assumes Protractor is installed in your node_modules/.bin/ directory locally in your project.
Next navigate to chrome://inspect. You will have to skip the entry point of Protractor and then you should hit your breakpoint. Make sure you are using node 8 to avoid getting polyfill-ed promises.
We go over this in detail in the ng-conf video. We kick off the test with a script that uses this e2e-debug command which is the equivalent to the above.

Nuclide debugger with node and babel?

I've been trying to use Nuclide/Atom to launch and debug a unit test that uses Babel and ES6+ code. The launch config looks like this:
Node runs the unit test as if I had run it from the command-line, and does not stop at my breakpoints. If I use the same invocation at the command-line with --inspect-brk I can debug correctly (with sourcemaps) from the chrome-devtools url in Chrome. Is there a way to make this work? I can't really "attach" since the unit tests are, and should be, a straight-shot script execution.
Nuclide currently does not support the new v8 Inspector protocol for debugging. You will need to use --debug flag to debug with Nuclide. Note, however, that the old debugger protocol has been removed from Node.js starting with Node.js 8.0.
PS. You can attach to a running Node.js process with Nuclide debugger just fine - just start your tests with node --debug --debug-brk ... and then attach the debugger from Nuclide. The test process will be stopped at first line, allowing you to resume execution at your own convenience.

Error while debugging NodeJs program using mocha

I have written some test cases and I am trying to debug the test cases using using mocha and node debug.
I have mocha installed on my machine
I installed node-debug npm install -g node-debug
Then I am running test by this command: node-debug _mocha test.js
This opens a browser window, but I am getting following error
Detached from the target
Remote debugging has been terminated with reason: Error: connect ECONNREFUSED. Is node running with --debug port 5858?
Please re-attach to the new target.
See the attached image for more information:
How can I get rid of this error. Please help.
That is expected behavior once your tests have finished running. Put a long timeout in your test so it does not die and try it again.
You will also want to run mocha with a very high timeout so your tests don't die timing out.
The node-debug project is deprecated.
If using node > 6.3, The debugger is a part of the node core!
see https://medium.com/#paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27#.3qx9qfmwl
If using node < 6.3, you can use node-inspector
Check out https://github.com/node-inspector/node-inspector
The rest should be smooth sailing if your test is not ending the process before you're done debugging. You can also set --debug-brk so it stops on the first line of your code to give us slow humans a chance to get to the debuggeer.
Best of luck!

Resources