VS Code launch.json to run and debug a MERN app - debugging

I'm trying to setup my debug environment on VS Code to run and debug a MERN app.
I currently have this launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node: Nodemon",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"outputCapture": "std",
},
]
}
It's working just fine, but with it I can only have breakpoints in the backend and not in the React app on the frontend.
The script yarn dev (package.json in the backend) runs both backend and frontend with concurrently:
Here's my scripts in package.json in the backend:
"scripts": {
"start": "node backend/server.js",
"server": "nodemon backend/server.js",
"client": "yarn --cwd frontend/ start",
"dev": "concurrently \"yarn server\" \"yarn client\""
}
What would be a working launch.json that would allow me to have breakpoints in the frontend as well?

One way to do this is to use VS Code's Multi-target debugging:
https://code.visualstudio.com/docs/editor/debugging#_multitarget-debugging
"Using multi-target debugging is simple: after you've started a first debug session, you can just launch another session. As soon as a second session is up and running, the VS Code UI switches to multi-target mode".
If you prefer to use launch.json, you can create a compound launch configuration, like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node: Nodemon",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"outputCapture": "std",
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceFolder}/client.js"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": [
"Node: Nodemon",
"Client"
],
"preLaunchTask": "${defaultBuildTask}",
"stopAll": true
}
]
}
The configurations will be launched in parallel.

Related

Ctrl+D (EOF) in debug mode VSCode

Version: 1.62.0
Commit: b3318bc0524af3d74034b8bb8a64df0ccf35549a
Date: 2021-11-04T20:38:29+02:00
Electron: 13.5.1
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
ОС: Solus 4.3 Fortitude, Linux x64 5.14.16-204.current
There is a program written in D.
In the loop, a string is fed to the input, read and further execution of the algorithm takes place.
When running in normal mode, the program works without any problems.
When running in debug mode, I can't continue running the program because it hangs on the input process. I can't continue executing my code after pressing "Enter".
My launch.json:
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "launch",
"name": "Launch Program",
"target": "./lesson_2/bin/lesson_2",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText"
}
]
I tried to connect an external terminal, but it didn't work out.
Did so:
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "launch",
"name": "Launch Program",
"target": "./lesson_2/bin/lesson_2",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText",
"console": "externalTerminal",
"debugOptions": [
"RedirectOutput"
]
}
]
And that's it:
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "launch",
"name": "Launch Program",
"target": "./lesson_2/bin/lesson_2",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText",
"externalConsole": true
}
]
I also tried adding this option, but nothing has changed:
"console": "integratedTerminal"
I use Native debug.
In general, I don't know what to do. I ask for help.

How to debug bootRun with VScode

I'm trying to debug a Spring bootRun application via VSCode. I'm not sure what the proper launch configuration is.
This is how I launch the program in a terminal
./gradlew bootRun -Dspring.profiles.active=local
These are the current configurations I've tried with no luck.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug",
"args": [
"bootRun",
"-Dspring.profiles.active=local"
],
"mainClass": "com.test.Application",
"request": "launch"
},
{
"type": "java",
"preLaunchTask": "gradle",
"name": "Debug Task",
"request": "attach",
"hostName": "localhost",
"port": 5005
}
]
}
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "gradle",
"type": "shell",
"command": "./gradlew",
"args": [
"bootRun",
"-Dspring.profiles.active=local",
"--debug-jvm"
],
"problemMatcher": []
}
]
}
The "Debug" configuration spits out the following error
No active profile set, falling back to default profiles: default
The "Debug Task" configuration runs the task, but it waits until the task finishes which it never will. So I can't debug it.
EDIT 1:
So if I run this task
{
"version": "2.0.0",
"tasks": [
{
"label": "gradle",
"type": "shell",
"command": "./gradlew",
"args": [
"bootRun",
"-Dspring.profiles.active=local",
"--debug-jvm"
],
"problemMatcher": []
}
]
}
Then run this launch configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "task 2",
"request": "attach",
"hostName": "localhost",
"port": 5005
}
]
}
I can debug the application, but this only attaches the debugger to the process. So I have to manually kill the process when I am done debugging. Ideally I would like to start and stop the application with vscode via a launch configuration.
EDIT 2:
I can achieve what I want in IntelliJ with this configuration, but I want to be able to do this in vscode.
EDIT 3:
This is my current configuration which works pretty well. I can start the program with CMD-SHFT-B then F5 to start the debugger.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug",
"request": "attach",
"hostName": "localhost",
"port": 5005
}
]
}
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "gradle",
"type": "shell",
"command": "./gradlew",
"args": [
"bootRun",
"-Dspring.profiles.active=local",
"--debug-jvm"
],
"dependsOn": [
"kill-java"
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "kill-java",
"type": "shell",
"command": "pkill",
"args": [
"java"
]
}
]
}
You can achieve that by adding the following in .vscode/settings.json file:
{
"gradle.javaDebug": {
"tasks": [
"bootRun"
],
"clean": true
}
}
After saving the file, next to Run Task will apear the Debug Task option in Gradle - Gradle Tasks view:
You need to have the Gradle Tasks, Debugger for Java and Language Support for Java extensions installed.
It's worth taking a look at https://github.com/badsyntax/vscode-gradle which will make this easier for you. You can debug your app, and restart the debugging in one step after making code changes.
I just right click on the main Spring Boot Application class (ie. as annotated with #SpringBootApplication) and click "Debug". That will spin up the server and allow you to halt at breakpoints. Seems to work OK for me.
(I have the Microsoft Java Extension Pack Plug-in installed)

Debugging strapi in Visual Studio Code

I'm trying to debug my strapi project (3.0.0 beta 16.6) in VS Code.
My launch.json:
{
"type": "node",
"request": "attach",
"name": "Attach to strapi",
"port": 9229
}
My package.json:
"scripts": {
"debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
}
Debugger attaches to the process, but all my breakpoints become unverified (appear black, not red). What's wrong with my configs?
this answer come from the following strapi/strapi issue:
I have come up with next solution:
having next script in server.js file (my custom one):
const strapi = require('strapi');
strapi({ dir: process.cwd(), autoReload: true }).start();
I'm using nodemon by next command: nodemon --inspect=0.0.0.0:9228 server.js
Now I can attach to 9228 by debugger.
Just to add to #alxnkt comment.
I have encountered the same and solved it by changing the launch.json to port 9230
{
"type": "node",
"request": "attach",
"name": "Attach to strapi",
"port": 9230
}
while keeping the port on package.json as 9229
"debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
There are somehow 2 processes running when calling the Strapi develop command (possibly the admin panel and its core server), the process we have to monitor becomes port 9230 instead.
Add VS configuration "NODE: launch via npm" in .vscode/launch.json "configurations" property and run
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"develop"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
],
"console": "integratedTerminal"
}
This worked for me using Strapi v4
{
"version": "0.2.0",
"configurations": [
{
"name": "STRAPI Debug",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "node",
"runtimeVersion":"14.19.0",
"runtimeArgs": ["--lazy"],
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceRoot}/node_modules/#strapi/strapi/bin/strapi.js",
"args": [
"develop"
],
"protocol": "inspector",
"env": {
"NODE_ENV": "development"
},
"autoAttachChildProcesses": true,
"console": "integratedTerminal"
},
]
}
The problem has been solved by setting port number to 9203:
{
"type": "node",
"request": "attach",
"name": "Attach to strapi",
"port": 9229
}
But I have no idea about HOW it works...
I just launched it via NPM, here is my launch.json (in .vscode folder)
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"develop"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
],
"console": "integratedTerminal"
}
]
}
You can also use NODE_OPTIONS:
NODE_OPTIONS='--inspect' yarn strapi develop
You'll get:
$ NODE_OPTIONS="--inspect" yarn strapi develop
Debugger listening on ws://127.0.0.1:9229/8564942a-6476-443a-9f64-87fa6d0055b7
For help, see: https://nodejs.org/en/docs/inspector
yarn run v1.22.5
$ strapi develop
Starting inspector on 127.0.0.1:9229 failed: address already in use
Debugger listening on ws://127.0.0.1:9230/7da840dd-cb89-493f-8ce0-e11530efdfbb
For help, see: https://nodejs.org/en/docs/inspector
Now you can use Debug: Attach to Node Process to attach to port 9230.
For strapi v4 you will have to add "sourceMap": true to compilerOptions of tsconfig.json file.

Debug Jest with Electron using VS Code

I can't manage to debug my Jest tests in VS Code when I use Electron. My tests should run with Electron, not with Node (due to the use of native modules).
{
"name": "Jest Unit Tests",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"program": "${workspaceRoot}/node_modules/.bin/jest",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"env": {
"ELECTRON_RUN_AS_NODE": "1",
"NODE_ENV": "test",
"BABEL_DISABLE_CACHE": "1"
},
"args": [
"-i",
"--verbose",
"-c test/config/jest.unit.json"
],
"internalConsoleOptions": "openOnSessionStart"
},
It is based on the usual config used to debug Jest with Node (which works fine), but I can't get it to work with Electron. The Jest command is correct, but the --debug-brk --inspect option added by VSCode seems to mess out with Jest.
I was able to get my config to work based on your question. Thanks!
{
"type": "node",
"request": "launch",
"name": "Debug Current Jest File:Electron",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"--verbose",
"-i",
"--no-cache",
"--testPathPattern",
"${fileBasename}",
"--coverage",
"false"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"env": {
"ELECTRON_RUN_AS_NODE": "true"
}
}

Breakpoints not being hit when debugging Serverless in vscode

None of my breakpoints are active when debugging my serverless based application in VSCode.
launch.json
{
"configurations": [
{
"console": "integratedTerminal",
"cwd": "${workspaceRoot}",
"name": "Debug",
"port": 5858,
"request": "launch",
"runtimeArgs": [
"run-script",
"vscode:debug"
],
"runtimeExecutable": "npm",
"type": "node"
}
],
"version": "0.2.0"
}
My package.json
...
"scripts": {
...
"vscode:debug": "export SLS_DEBUG=* && node --inspect=5858 --debug-brk --nolazy ./node_modules/.bin/serverless invoke local -s local -f customerAlexa -p ./test/requests/FindAgent-First-GoodZip.json"
},
....
When I choose Start Debugging from the menu, all the red breakpoints go grey and the program just executes without stopping on the breakpoints.
I am running Node 6.11.2, Serverless 1.23.0 on a Mac. Thanks all.
Here is my launch.json which allows me to use breakpoints.
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/node_modules/.bin/serverless",
"args": [
"offline",
"start",
"--skipCacheInvalidation"
],
"env": {
"NODE_ENV": "development"
}
}
I am using the serverless-offline to run locally. I also am using webpack and babel. The skipCacheInvalidation is for that.
I hope this points you in the right direction.

Resources