gpt4 book ai didi

node.js - 在 VS Code 中使用 nodemon 调试 nestJS 应用程序

转载 作者:行者123 更新时间:2023-12-02 00:07:00 25 4
gpt4 key购买 nike

有没有办法用 nodemon 调试 nestJS 项目。

我在 launch.json 中试过这段代码

    {
"type": "node",
"request": "launch",
"name": "NestJs Watch",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "start:dev"],
"cwd": "${workspaceFolder}",
"port": 3000
}

但是我得到了这个错误

和我的 nodemon.json 文件

{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node -r --inspect=3000 tsconfig-paths/register src/main.ts"
}

最佳答案

如果我们想在 Debug模式下工作,有更好的机会看到代码中发生了什么,我们需要使用“nodemon”和专用的“nodemon.json ”配置文件来运行我们的开发“nestjs”服务器,其中 ts-node 模块连接 typescript 编译器。

对我有用的步骤是:

  • 安装 nodemon 和 ts-node:

npm i --save-dev nodemon ts-node

  • 接下来,在项目的根目录中添加一个带有调试和 ts-node 支持的 nodemon.json 文件:

file: (project root) nodemon.json

  • 并插入此配置。 JSON 文本:
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "node --inspect-brk -r ts-node/register src/main.ts"
}
  • 接下来调整文件:package.json - 部分:“start:debug”

file: (project root) package.json

  • 原始值通常是:
...
> "start:debug": "nest start --debug --watch",
...
  • 将其更改为:
...
> "start:debug": "nodemon --config nodemon.json"
...
  • 现在,在 VSC (Visual Studio Code) 中确保您可以在最底部的状态栏上看到:

    "Auto Attach: On"

如果没有,请在键盘上按以下键:

Ctrl + Shift + p

打开命令面板,并粘贴此命令:

Debug: Toggle auto attach

然后按回车键。

现在您应该看到:

"Auto Attach: On"

  • 现在使用断点调试应该可以了。

  • 首先在程序代码的开头放置一个断点
    (以确保流程不会在您的断点之前结束...)

file: (project root) 'main.ts'


> function: bootstrap() {

console.log('test'); // -- place break point here

// ... other code ...
}
  • 在 VSC (Visula Studio Code) 中选择菜单项:

Start debugging (or F5)

并在弹出菜单中选择 Node.js 作为环境选项。

现在应该在 bootstrap() 函数中捕获断点。

关于node.js - 在 VS Code 中使用 nodemon 调试 nestJS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60285180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com