I am testing the nest.js framework but I am struggling to run it with VSCode so that I can properly debug my code. This is pretty much the same issue as described here Running n
What I did was auto attach the vs code debugging process with one of my scripts in package.json. And on top of it I used nodemon, which would automatically restart up along with the debugger, if you have made any changes in development.
The process requires you to install nodemon globally and add a nodemon-debug.json file in the root of the folder, which looks like this.
nodemon-debug.json
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register src/main.ts"
}
Then in the package.json, add a script that looks like this
"debug": "nodemon --config nodemon-debug.json"
Then in the VS Code, hit F1 > search for Debug: Toggle Auto Attach. Hit on it to enable it.
Then start up the debugging process by running the following command -
npm run debug
The debugger automatically turns on.
The advantage of this process is nodemon, which automatically starts along with the debugger every time you make some changes in the code and need it up.
For a more detailed explanation, go through this link. Worked for me.
If you want to have log output in terminal + all debug capabilities, you can start + attach using npm, here is config for launch.json:
{
"type": "node",
"request": "launch",
"name": "Nest Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"start:debug",
"--",
"--inspect-brk"
],
"console": "integratedTerminal",
"restart": true,
"protocol": "auto",
"port": 9229,
"autoAttachChildProcesses": true
},
Will show full log output in console + debugging
No need to mess up with .vscode/launch.json
, just follow the official Auto Attach intro and ... Just Works!
For example, I wanna debug my project quiz
:
Run the app as usual, for this project is npm run server:dev
When the app started successfully, attach to the process
I just have to "promote" this comment by @JWess to a real answer (and update it with the current location of the relevant setting) so it may be found more easily (this worked for me out of the box for a newly generated nest project, without changing any other configuration or file):
If you go to Settings > Extensions > Node debug
and look for the setting Debug › Node: Auto Attach
and turn it on, VSCode will auto-attach when you run npm run start:debug
(i.e. nest start --debug --watch
) in the integrated terminal.
Try this launch.json:
{
// 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",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"args": ["${workspaceFolder}/src/main.ts"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
I tried the accepted answer and all the other variances and none worked for me, however what really works for me is to attach to the 9229 port. What I did is add/modify your launch.json
with the following config
.vscode/launch.json
{
// 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",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach NestJS WS",
"port": 9229,
"restart": true,
"stopOnEntry": false,
"protocol": "inspector"
}
]
}
and in package.json
(Nest new CLI commands, requires 6.8.x
, see this blog)
{
"name": "nest-app",
"scripts": {
"start:debug": "nest start --debug --watch"
}
}
and finally it works!