VSCode's Auto Attach is not toggling for Node.js - 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?

Related

Debug node.js backend application with an IDE, e.g. WebStorm

I added in package.json this into scripts:
"debug": "NODE_PATH=src nodemon --exec babel-node src/run.js --inspect",
It is pretending that it is debugging but jumping on selected lines how it pleases. I'm not that "expert" in JavaScript (I'm Java) but really this is pain in ass.
How can I debug backend without debug? Srry I'm angry because this is second time I'm giving change to JavaScript and this is second time I'm furious about this stupidity.
Thanks for any hints.
P.S.: if there is better software, tool to debug please just refer to it
In WebStorm, the easiest way to debug the application started via NPM stript is using the icon in the gutter: open your package.json in editor, right-click the icon to the left of your script and choose debug:
See also https://blog.jetbrains.com/webstorm/2017/09/debugging-node-js-apps-in-webstorm/
Note that:
babel-node is deprecated and not recommended for using in production. To get ES6 code compiled on-the-fly, try running node with -r #babel/register. see https://babeljs.io/docs/en/babel-register
by running nodemon --exec babel-node src/run.js --inspect, you pass --inspect to your application, not to node interpreter, so this command doesn't start the debugger. Node options have to be specified before the javascript file, otherwise the passed options will be treated as application arguments, not as node.js args
Right now, VSCode(Visual Studio Code) is considered as best tool for development, which is light weight and user friendly.
You can get different extensions based on your requirement.
To debug node js in vscode, go to debug window -> add configuration -> type attach process -> press ctrl + space -> press enter on attach suggestion.
After that, run command, node "your file name" --inspect
Press F5, your debugger will be attached
Node js debugging using VSCode

WebStorm debugger does not attach on first time

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.

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.

Chrome DevTools for node: How to stop in specific file

I try to debug a grunt task with Chrome DevTools on Windows using the following command
node --inspect-brk node_modules\grunt\bin\grunt
This works nicely and the Chrome debugger stops in the root grunt javascript. But of course, I do not want to debug grunt, but my own "gruntfile.js". How can I set a breakpoint into that file, as it is not loaded yet?
I tried to add my project folder in the Chrome DevTools under "FileSystem", but whatever I add there as a breakpoint does not stop.

How to configure forever in nodeclipse?

I have installed "Enide Studio 2014" on Windows 7. Then I created an "Node.js Express Project". Everything went smoothly, a new project was created. I was able to run it without any problems.
As stopping and starting node server everytime I make any changes in the code takes unnecessary efort while developing, I wanted eclipse to "monitor" changes and restart node accordingly. As nodeclipse provides an option for this purpose, I tried to configure forever as node monitor.
In Nodeclipse preferences Node Path was set as "D:\Software\Development\NodeJS\node.exe" as it should be. Forever was installed in "D:\Software\Development\NodeJS\node_modules\forever\bin\forever" so I set "Node monitor path" in as that.
After this I was not able to run the project again.
When I check console output, the command issued (and failed) was:
node D:\Software\Development\NodeJS\node_modules\forever\bin\forever D:\calismaalani\nodejs\deneme\app.js
I copied the command line and issued it on the command window myself and got:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Cannot start forever
error: script D:\Software\Development\NodeJS\D:\calismaalani\nodejs\deneme\app.js does not exist.
As it is clearly seen, Nodeclipse was trying to add node_home path to the absolute path for my app. I have tried to set some environment variables but could not manage to get the right path for my app.
What is wrong here? How can I fix this?
Since Nodeclipse 0.17 Run with Node monitor is hidden as it raised more questions then solved.
Please launch via CLI as forever and TCF-Terminal is nice shell integration into Eclipse,
so you can have several forever instances running for different project in separate tabs.
You can specify the "Node options" path in Nodeclipse preferences as D:\Software\Development\NodeJS\node_modules\forever\bin\forever --sourceDir . and Nodeclipse should automatically use forever to start your node Js application.
I use nodeclipse version 1.0.2.x and use nodemon to monitor the application and it works fine for me.
Edit:
In your case, you cannot pass the absolute path to forever . if you need to ,you can use the "--sourceDir . " option of forever. The --sourceDir and the "." option looks for the full path of the application. So your command will look as follows:
node D:\Software\Development\NodeJS\node_modules\forever\bin\forever --sourceDir . D:\calismaalani\nodejs\deneme\app.js
You need to give Node monitor path as D:\Software\Development\NodeJS\node_modules\forever\bin\forever --sourceDir .
Incase you are using nodemon, you need to give the path as C:\Users\ashwin_valento\AppData\Roaming\npm\node_modules\nodemon\bin\nodemon.js --watch

Resources