English 中文(简体)
VS 代码中带有点头程序的调试巢JS 应用程序
原标题:debug nestJS application with nodemon in VS Code

是否有办法用点头程序调试一个巢 JS 项目 。

我在发射时尝试过这个代码.json

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

但我有这个错误

和我的记事程序。 json 文件

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

如果我们想要在调试模式下工作, 并且有更好的机会看到代码中正在发生的事情, 我们需要使用“ nodimon ”, 配有专门“ em>nodimon.json ” 配置文件, 运行我们的开发“ < em> nestjs ” 服务器, 并使用 ts- node 模块连接到编译器中 。

对我有效的步骤是:

  • Install nodemon and ts-node:

npm i -- save- dev 节点

  • Next, add a nodemon.json file with debug and ts-node support in the root of your project:

文件:( 项目 root) 点头程序. 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"

文件:( 项目 root) 套件.json

  • The original value typically is:
...
> "start:debug": "nest start --debug --watch",
...
  • Change it to:
...
> "start:debug": "nodemon --config nodemon.json"
...
  • 现在,在VSC(视频工作室代码)中,请确保您可以在底部状态栏上看到:

    "自动随员: 开"

如果不是,在键盘上按键:

Ctrl + Shift + p

打开命令调色板,并粘贴在此命令中:

调试: 切换自动附件

并按 Enter 键。

现在你应该看到:

"自动随员: 开"

  • 现在用断点调试应该有效。

  • Start with placing a break point in the beginning of your program code
    (to make sure the flow does not end before your breakpoint ...)

文件:( 项目根) 主. ts


> function:  bootstrap() {

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

  // ... other code ...
}
  • In VSC (Visula Studio Code) select menu item:

开始调试 (或 F5)

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

中断点现在应该按照靴子陷阱() 功能捕捉 。

我执行此命令 :

npm run start:dev

尝试此节点配置 :

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

然后您就可以运行: nodimon -- config nodimon.json

仅对 Nodejs 使用 Nodimon 。 在 Nestjs 中, 要使用热重新装入, 需要做以下操作 :

STEP1: 首先安装所需的包件 :

 npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack

STEP2: 在您应用程序的根目录中创建 < strong> webpack- hmr.config.js 文件

添加代码 :

const nodeExternals = require( webpack-node-externals );
const { RunScriptWebpackPlugin } = require( run-script-webpack-plugin );

module.exports = function (options, webpack) {
  return {
    ...options,
    entry: [ webpack/hot/poll?100 , options.entry],
    externals: [
      nodeExternals({
        allowlist: [ webpack/hot/poll?100 ],
      }),
    ],
    plugins: [
      ...options.plugins,
      new webpack.HotModuleReplacementPlugin(),
      new webpack.WatchIgnorePlugin({
        paths: [/.js$/, /.d.ts$/],
      }),
      new RunScriptWebpackPlugin({ name: options.output.filename, autoRestart: false }),
    ],
  };
};

STEP3: 为了启用高管关系系统,打开应用程序条目文件(main.ts ),并添加以下与网页包有关的说明:

declare const module: any;

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);

  if (module.hot) {
    module.hot.accept();
    module.hot.dispose(() => app.close());
  }
}
bootstrap();

< 加固> STEP4: 要简化执行过程, 请在您的软件包.json 文件中添加脚本 。

"start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch"

<% 1> STEP5: 现在只需打开命令行并运行以下命令 :

 npm run start:dev

<强度>STEP6: 最终 Ctrl+S 到热装入





相关问题
Eclipse: Hover broken in debug perspective

Since upgrading Eclipse (Galileo build 20090920-1017), hover in debug no longer displays a variable s value. Instead, hover behaves as if I were in normal Java perspective: alt text http://...

IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Asp.Net MVC - Problem returning a model to the view

I ve recently started using the Areas functionality of the Preview2 and it was working ok until I needed to return a Model to the view. Controller: public ActionResult ForgotPassword() { return ...

Unable to generate PDB files in Visual Studio 2005

For most assemblies, I m able to generate their respective .pdb files. However, I ve got one project that isn t generating their .pdb files. I ve made sure that it on debug mode and that the project ...