VS Code settings to launch frontend and backend for debug - node.js

I'm trying to get a simple react frontend and nodejs backend up and running and debuggable in vs code. I'm using a compound launch configuration to launch 'client' and 'server' together. The nodejs backend is started automatically for me, but I always have to do an npm start in the console for the client for it to connect. All the tutorials I've seen suggest that this step has to happen before running the debug configuration in vs code. Is it not possible for vs code to start the frontend just like it does for the nodejs backend.
This is what my launch.json looks like:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"compounds": [
{
"name": "Client+Server",
"configurations": [ "Server", "Client" ]
}
],
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceFolder}/server/server.js"
},
{
"type": "chrome",
"request": "launch",
"name": "Client",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}/client/src/index.js"
}
]
}

I had some trouble launching the debug session from beginning so I decided to start the nodejs server then attach the debuggers.
I found this configuration working for me.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach Server",
"restart": true,
"port": 9000
}, {
"type": "chrome",
"request": "launch",
"name": "Launch Client",
"port": 9001,
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}/dist",
"sourceMaps": true
}
],
"compounds": [
{
"name": "Attach Client+Server",
"configurations": ["Attach Server", "Launch Client"]
}
]
}
And here are some of the scripts I used for package.json.
{
"scripts": {
"prestart": "rollup -c -w",
"start": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index"
},
}
I had to use nodemon to detect changes and restart the node server.
But if your React application need to be started separately from your node application then I suggest using something like http-server to start the local server for your React app. And then use concurrently to start both applications concurrently.
Your package.json may look like the following:
{
"scripts": {
"prestart": "rollup -c -w",
"start:client": "http-server ./dist/client/",
"start:server": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index",
"serve": "concurrently \"npm:start:client\" \"npm:start:server\""
},
}
Resource VS Code debug configuration: https://code.visualstudio.com/Docs/editor/debugging

The reason that the client requires an npm start is because you are referring to http://localhost:3000
npm start will actually run a mini web server to serve up your html files on http://localhost:3000
The other way is to use something like http-server on your src, which will have the same effect

Related

How to debug Typescript code in Visual Studio Code with ts-node-dev and correct line numbers

I have a Typescript project that is launched as follows:
ts-node-dev --preserve-symlinks --inspect=0.0.0.0 -- src/server.ts
I can debug it with Visual Studio Code, but the debugger breaks at the wrong lines. The only reasonable explanation I can think of, is that ts-node-dev does not point the debugger to the source maps (which are there).
How can I correctly debug Typescript code executed by ts-node-dev?
Configuration for debugging in vs code with ts-node-dev to attach and launch debugger:
package.json:
{
"scripts": {
"dev:debug": "ts-node-dev --transpile-only --respawn --inspect=4321 --project tsconfig.dev.json src/server.ts",
"dev": "ts-node-dev --transpile-only --respawn --project tsconfig.dev.json src/server.ts",
}
}
launch.json:
{
"version": "0.1.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to dev:debug",
"protocol": "inspector",
"port": 4321,
"restart": true,
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "launch",
"name": "Debug",
"protocol": "inspector",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "dev"]
}
]
}
I had the same question, which brought me to this (old) post. I found the solution to my problem at https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695 so am posting it here in case anyone else finds themselves here!
This VS Code configuration allowed me to stop at breakpoints on the correct lines in my TypeScript code:
{
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"args": ["src/script.ts", "--example", "hello"],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"]
}
]
}
This is what worked for me. attach type debugger was not working for me but launch type is working fine. Breakpoints works as well even though sometimes it goes to the ts-node-dev source files and I guess we can't do anything about it.
It runs tsnd which is just the alias for ts-node-dev.
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"name": "launch",
"request": "launch",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/tsnd",
"program": "${file}",
"args": [
"--transpile-only",
"--respawn",
"--project",
"tsconfig.dev.json",
],
"skipFiles": [
"<node_internals>/**"
],
},
}
"program" could be changed to run a specific file by replacing ${file} with that filename but with the above config, it will run the opened file with the debugger.

VSCode debug configuration for Next.js Frontend with Node.js backend

In my project directory I have a frontend in next.js and a backend in Node.js it looks as follows:
I now would like to debug the program, so both backend and frontend have to run, and I don't know how to create the corrent debug launch.json configuration which I have in my top-level project, to launch and debug both programs.
{
"version": "0.2.0",
"compounds": [
{
"name": "Client+Server",
"configurations": [ "DCBACKEND", "DCFRONTEND" ]
}
],
"configurations": [
{
"type": "node",
"request": "launch",
"name": "DCBACKEND",
"cwd" : "${workspaceFolder}\\dcbackend",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script", "start"
]
},
{
"type": "node",
"request": "launch",
"name": "DCFRONTEND",
"cwd" : "${workspaceFolder}\\dcfrontend",
"program": "${workspaceFolder}\\dcfrontend\\pages\\index.js",
"runtimeExecutable": "next",
"runtimeArgs": [
"--inspect"
]
}
]
}
EDIT: Now, I get the following error:
My start script for my backend is:
"scripts": {
"start": "node index.js -p 7766"
},
so there is something wrong with the port maybe? Also, it runs another script for debug in cmd-line:
C:\Program Files\nodejs\npm.cmd run-script start --inspect-brk=39777
I don't know what it has to do with --inspect-brk=39777, do I need that

How to debug multiple node projects in vscode debugger?

I have two separate node projects in two separate workspaces. I am trying to debug the projects with vscode debugger but I can only debug one project at a time. If I try to launch the debugger for the second project after starting the debugger for the first one, vscode debuggers restarts the first project again.
I have gone through various tutorials and vscode documentation for debugging and vscode debugging for nodejs but to no avail. Following are the launch configurations for both projects.
Project 1(fort):
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch fort",
"runtimeExecutable": "npm",
"runtimeArgs": [
"start"
],
"envFile": "${workspaceFolder}/.env",
"port": 9229
}
]
}
Value for scripts attribute in package.json
"scripts": {
"start": "node --inspect app.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
Project 2(User Management):
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch User Management",
"runtimeExecutable": "npm",
"runtimeArgs": [
"start"
],
"envFile": "${workspaceFolder}/.env",
"port": 9229
}
]
}
Value for scripts attribute in package.json
"scripts": {
"start": "node --inspect server.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
As per my understanding after reading the vscode docs, if I have separate launch.json present in the .vscode folder of the workspaces that particular configuration will be used to launch the debugger.
Perhaps I am missing something in the docs but I have invested enough time and have been unable to figure out a solution.
You need to use two separate ports for the debuggers to attach, e.g:
Project 1:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch fort",
"runtimeExecutable": "npm",
"runtimeArgs": [
"start"
],
"envFile": "${workspaceFolder}/.env",
"port": 9228
}
]
}
Or if you want to attach to the process:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9228
}
Start node inspect on port 9228:
node --inspect=9228 index.js
You can keep the defaults for the second project.

Debug grommet-cli sample-app in vscode

Loving Grommet and vscode. Trying to get them to play together. I am able to get Express and vscode working as shown here. I would like to get the grommet-cli sample app to work similarly. Express has one command to start: "npm start" where grommet-cli has two: "npm run dev-server" and "npm run dev" (not sure how to start them both in vscode. I think I may need multi-session debugging?). How do I setup launch.json to debug the sample app? I'd like to be able to debug in IE/Edge. I've had some success in Chrome with the Debugger for Chrome extension.
Here is my current launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceRoot}"
},
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 9222,
"webRoot": "${workspaceRoot}"
}
]
}
This appears to resolve the issue. Hopefully, it's been added to the latest version of VS.
"compounds": [
{
"name": "Node+Browser",
"configurations": [ "Server", "Browser" ]
}
],
"configurations" [
{
"name": "Browser",
"type": "chrome",
//...
},
{
"name": "Server",
"type": "node",
//...
}
]

How can I make VSCode run a node app like "npm start"?

I have a node app with the package.json like so:
{
"name": "myexpressapp",
"version": "0.0.99",
"scripts": {
"start": "node ./bin/www" },
//etc
}
and in app.js I have
console.log ("Running version: "+process.env.npm_package_version);
When I run npm start in the command prompt this logs
Running version: 0.0.99
When I start (press F5) in VS Code this logs
Running version: undefined
What change do I need to make to launch.json to start the app in VS Code as if I had entered npm start in the command prompt?
I can now get it to run (but not debug) using the following launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM#3",
"runtimeExecutable": "npm",
"windows": {
"runtimeExecutable": "npm.cmd"
},
"runtimeArgs": [
"start"
],
"port": 5858,
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program#1",
"program": "app.js",
"cwd": "${workspaceRoot}"
}
]
}

Resources