Vscode debugger - auto-attach when process restarts through pm2 - node.js

I am experimenting with different environments for debugging a nodejs application. I am running this application using pm2, with the --watch flag, so that my application restarts when files are changed / saved. Relevant scripts are these:
"start": "node --inspect -r tsconfig-paths/register -r ts-node/register ./src/server.ts",
"launch": "pm2 start pm2.config.js",
And my pm2.config.js looks like this:
module.exports = {
apps: [
{
name: 'myapp',
script: 'npm',
args: 'start',
watch: ['src'],
},
],
};
So far, I have been doing debugging in the chrome node inspector. I'm also using the chrome extension NodeJS Inspector Manager, which is a nice wrapper for the node inspector. It has a nice UI, and you can see there are options for Auto Attach and Auto Resume:
With these enabled, any time I save files in my project, pm2 will restart the application. NiMs automatically detaches and reattaches to the running application, and I have an interactive chrome inspector debugging environment that refreshes through application restart.
I'd like to reproduce this effect within the vscode debugger. I can manually start a debug session that attaches to my application running through pm2. With some help from Debugging With PM2 And Vscode, I'm able to direct the vscode debugger to attach to my application running through pm2. However, when I save files, pm2 restarts the process, and vscode debugger detaches, but does not reattach.
NiMs has a vscode extension, though it doesn't seem to offer the same functionality within vscode as within chrome, as far as I can tell.
Is there a way to set up a vscode debugger to attach to a pm2 process, and automatically restart as the process restarts (on the same debugger port)?

To have vscode debugger re-attach automatically, you can add the flag restart to your configuration:
{
"name": "Attach to node",
"type": "node",
"request": "attach",
"restart": true,
"port": 9229
}
source: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_restarting-debug-sessions-automatically-when-source-is-edited

Related

VSCode NodeJs: Debugger not stopping at breakpoint (WSL2/Ubuntu18)

Using WSL2/Ubuntu18 I've not been able to make the VSCode NodeJs Debugger to stop on the breakpoints of any NodeJs app. When I start the debugger, it runs (I can see the output on the integrated terminal) but breakpoints are simply ignored.
The simple.js file, with a breakpoint on line 3:
The launch.json is set to:
{
"version": "0.2.0",
"configurations": [
{
"name": "NodeJs: Launch Program",
"program": "${file}",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node",
"console": "integratedTerminal"
}
]
}
When I press F5 or click on the "Start Debugging" button on VS Code, the app runs and following appears on the integrated Terminal:
/usr/bin/env 'NODE_OPTIONS=--require /home/myuser/.vscode-server/bin/ea3859d4ba2f3e577a159bc91e3074c5d85c0523/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.19338-1.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/myuser/.nvm/versions/node/v14.15.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-ff32d873905abafa"}' /home/myuser/.nvm/versions/node/v14.15.1/bin/node ./simple.js
Debugger attached.
0
1
2
3
4
Waiting for the debugger to disconnect...
I've already upgraded from Node10 to Node14, but the problem persists.
On another computer using WSL1, and using the same launch.json the debugger stops at the given breakpoints. Do I need to set something additionally on WSL2? For the record, this is what appears on the integrated terminal on the WSL1 computer before it stops at line 3:
/usr/bin/env 'NODE_OPTIONS=--require /home/myuser/.vscode-server/bin/ea3859d4ba2f3e577a159bc91e3074c5d85c0523/extensions/ms-vscode.js-debug/src/bootloader.bundle.js --inspect-publish-uid=http' 'VSCODE_INSPECTOR_OPTIONS={"inspectorIpc":"/tmp/node-cdp.787-3.sock","deferredMode":false,"waitForDebugger":"","execPath":"/home/myuser/.nvm/versions/node/v14.15.1/bin/node","onlyEntrypoint":false,"autoAttachMode":"always","fileCallback":"/tmp/node-debug-callback-b901b6d6e3e9799b"}' /home/myuser/.nvm/versions/node/v14.15.1/bin/node ./simple.js
Debugger attached.
<Breakpoint hit and stop...>
Additional info, debugging Python3 files work correctly on both machines.
Both computers have the same VS Code Version installed.
Update:
You can follow the issue on GitHub: https://github.com/microsoft/vscode/issues/113283
The problem is that the NodeJs App is being run from a symlinked address - so the debugger can not handle it.
Answer from one of the developers of VSCode/NodeJS on github:
It looks like you have your script symlinked into /bin/nhosko/simple.js, but its actual location is /mnt/c/Users//bin-nhosko/simple.js. In this case you need to specify some flags so that Node will report the linked location that vscode sees and told the debugger about: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_can-i-debug-if-im-using-symlinks. I want to make the debugger smart enough to fix this automatically in microsoft/vscode-js-debug#776.
https://github.com/microsoft/vscode/issues/113283#issuecomment-750371948

How to debug a Node.js server? The debugger skips my breakpoints! (Using VSCode)

I'm trying to debug a Next.js app with a custom server which I normally run with a dev Yarn script that executes node server.js.
VSCode includes a Node.js debugging extension and this guide, which I've followed. I created a new Yarn script called dev:debug that runs node --inspect server.js, and the following configuration in launch.json:
{
"type": "node",
"request": "launch",
"name": "Debug via Yarn",
"runtimeExecutable": "yarn",
"runtimeArgs": ["dev:debug"],
"port": 9229
}
However some breakpoints in modules imported by server.js are skipped and I'm not sure why. Other breakpoints in Next components do work when I load pages in my web app. Any ideas?
OK! Here's what's happening (and I'm posting all this to SO because I couldn't find the answer online):
When you build a Next.js app, you're writing BOTH the server app (node), AND the server-side web app (next), which are separate things! Additionally, you're also building a client-side app (React) on top of that. (If you're used to other platforms like PHP or Python web apps that use servers like the built-in ones, Apache, or NginX, this isn't obvious!)
Running node --inspect server.js tells the debugger to load the server and THEN start debugging your web app ONLY ("user code").
Solution: node CLI has a special --inspect-brk option to use when you also want to debug the code of the server itself! Yep, the solution is 4 more characters (-brk).

Debug Mocha tests being executed via NPM in terminal of VSCode

Does anyone know how to configure VSCode to debug Mocha tests when executing via a test script? Setup is:
"test" config in package.json of project specifying the mocha command to execute( mocha -R mochawesome -s 3000 -t 30000 ./index.js )
'npm test' command used in internal terminal of VSCode with '-g' param to allow for execution of specific descriptions within CoffeeScript test files
I want to be able to debug the execution of these individual tests(i.e. run 'npm test -- -g "test description"' in VSCode and break in VSCode's Debug view when it reaches a bp). Is this possible? Would an 'attach' config be needed instead of 'launch'?
I've tried the standard debug configs provided in VSCode , and tried to modify them based on info found in various places, but no success so far. Any help would be great, not too familiar with the IDE, or any of these processes Thanks!
Late answer but it may help people falling on this question.
Adding inspect-brk will hold the process until you connect your debugger, vscode in this case. After that tests will run and stop on your debug points. Usually the listening port for debugging is 9229, but you'll see the correct port on the sysout.
mocha --inspect-brk test.js
Credits to Run node inspector with mocha
You can attach vs code debugger to a process launched by script
For that you need to:
1) Add mocha's --inspect option to your script
2) Configure your launch.json this way
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Mocha: tests",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector",
},
]
3) After running your script hit F5 and pick mocha's process from vs code popped up processes list (you need to be quick here :) )
4) Second time you run the script and hit F5 vs code will automatically pick the right process for you

WebStorm Node.js TypeScript Express debug stuck on connecting to localhost

I'm trying to debug my TypeScript Express app using WebStorm.
I have this debug script in package.json
"scripts": {
...
"debug": "node --inspect-brk=9229 --require ts-node/register -r tsconfig-paths/register server.ts"
}
I run npm run debug in the command line and the following loads
Debugger listening on ws://127.0.0.1:9229/<somerandomid>
For help see https://nodejs.org/en/docs/inspector
Now I am able to debug from Chrome inspector by going to Chrome, type in chrome://inspect/ and server.ts would appear in Remote Target, and I can debug the TypeScript by clicking inspect. Hence I know that the problem is not my node.js configuration side.
The problem is, I can't debug when using WebStorm.
I tried the following WebStorm debug configuration:
Attach to Node.js/Chrome
Host: localhost
Port: 9229
Attach to: Chrome or Node.js > 6.3 started with --inspect
but when I debug in WebStorm the debugger keeps on saying "Connecting to localhost:9229" and nothing happens. It doesn't go to breakpoints even though I have set breakpoints in server.ts etc
I tried disabling firewall, still doesn't work. Tried using --inspect instead of --inspect-brk, still doesn't work.
What am I doing wrong, and how can I get WebStorm to debug into breakpoints using my node.js Express TypeScript configuration?
Works fine for me using your way to start the app/attach the debugger. What WebStorm version do you use?
here are 2 other ways to debug your app:
using Node.js run configuration (create similar configuration and press Debug):
using NPM run configuration:
change your script to "debug": "node %NODE_DEBUG_OPTION% --require ts-node/register -r tsconfig-paths/register server.ts" (if you are on Linux/Mac OSX, replace %NODE_DEBUG_OPTION% with $NODE_DEBUG_OPTION
click the arrow in the gutter, choose Debug

Issues setting up remote node debugging with WebStorm

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

Resources