在VS Code中使用nodemon调试nestJS应用程序 [英] debug nestJS application with nodemon in VS Code
问题描述
有没有一种方法可以使用nodemon调试nestJS项目.
我在launch.json中尝试了这段代码
{"type":节点","request":启动","name":"NestJs Watch","runtimeExecutable":"npm","runtimeArgs":["run-script","start:dev"],"cwd":"$ {workspaceFolder}",端口":3000}
但是我遇到了这个错误
和我的nodemon.json文件
{"watch":["src"],"ext":"ts","ignore":["src/**/*.spec.ts"],"exec":"ts-node -r --inspect = 3000 tsconfig-paths/register src/main.ts"}
如果我们想在调试模式下工作,并且有更好的机会查看代码中发生了什么,我们需要使用" nodemon "和一个专用的" nodemon.json "配置文件,以运行我们的开发" nestjs "服务器,而ts-node模块则连接了打字稿编译器.>
对我有用的步骤是:
- 安装nodemon和ts-node:
npm i --save-dev nodemon ts-node
- 接下来,在项目的根目录中添加具有debug和ts-node支持的nodemon.json文件:
文件:(项目根目录)nodemon.json
- 并插入此配置.JSON文字:
{"watch":["src"],"ext":"ts","ignore":["src/**/*.spec.ts"],"exec":节点--inspect-brk -r ts-node/register src/main.ts"}
- 下一个调整文件:package.json-部分:"start:debug"
文件:(项目根目录)package.json
- 原始值通常是:
...>"start:debug":嵌套启动--debug --watch",...
- 将其更改为:
...>"start:debug":"nodemon --config nodemon.json"...
-
现在,在VSC(Visual Studio代码)中,确保您可以在最底部的状态栏上看到:
自动附加:开"
如果不是,请在键盘上按以下键:
Ctrl + Shift + p
打开命令面板,并粘贴以下命令:
调试:切换自动附加
然后按Enter.
现在您应该看到:
自动附加:开"
-
现在可以使用断点进行调试了.
-
首先在程序代码的开头放置一个断点
(以确保流程不会在您的断点之前结束...)
文件:(项目根目录)"main.ts"
>函数:bootstrap(){console.log('test');//-在此处放置断点//...其他代码...}
- 在VSC(Visula Studio代码)中,选择菜单项:
开始调试(或F5)
,然后在弹出菜单中选择Node.js作为环境"选项.
现在应该在bootstrap()函数中捕获断点.
Is there a way to debug a nestJS project with nodemon.
i tried this code in launch.json
{
"type": "node",
"request": "launch",
"name": "NestJs Watch",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "start:dev"],
"cwd": "${workspaceFolder}",
"port": 3000
}
but i got this error
and my nodemon.json file
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node -r --inspect=3000 tsconfig-paths/register src/main.ts"
}
If we want to work in the debug mode, with a better chance to see what is happening in the code, we need to use "nodemon" with dedicated a "nodemon.json" configuration file to run our development "nestjs" server with the ts-node module hooking up the typescript compiler.
The steps that worked for me are:
- Install nodemon and ts-node:
npm i --save-dev nodemon ts-node
- Next, add a nodemon.json file with debug and ts-node support in the root of your project:
file: (project root) nodemon.json
- and insert this config. JSON text:
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "node --inspect-brk -r ts-node/register src/main.ts"
}
- Next adjust file: package.json - section: "start:debug"
file: (project root) package.json
- The original value typically is:
...
> "start:debug": "nest start --debug --watch",
...
- Change it to:
...
> "start:debug": "nodemon --config nodemon.json"
...
Now, in VSC (Visual Studio Code) make sure that you can see on the very bottom status bar:
"Auto Attach: On"
if not, on your keyboar press the keys:
Ctrl + Shift + p
to open the Command Palette, and paste in this command:
Debug: Toggle auto attach
and press Enter.
Now you should see the:
"Auto Attach: On"
Now debugging with break points should work.
Start with placing a break point in the beginning of your program code
(to make sure the flow does not end before your breakpoint ...)
file: (project root) 'main.ts'
> function: bootstrap() {
console.log('test'); // -- place break point here
// ... other code ...
}
- In VSC (Visula Studio Code) select menu item:
Start debugging (or F5)
and select Node.js as the Environment option in the popup menu.
The breakpoint should be caught now in the bootstrap() function.
这篇关于在VS Code中使用nodemon调试nestJS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!