What is the proper way to debug an npm script using vscode?

前端 未结 1 748
长发绾君心
长发绾君心 2020-12-16 09:46

I\'ve got an npm script that I\'m trying to debug. I use vscode so I thought I\'d create a debug configuration and step through it with the debugger.

My npm script

1条回答
  •  天涯浪人
    2020-12-16 10:35

    You shouldn't try to debug the npm script because what you really want is to attach your debugger to the script that is launched with npm run command (NPM here is used only as a task runner).

    {
      "version": "0.2.0",
      "configurations": [
          {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceRoot}/tasks/runner.js"
          }
      ]
    }
    

    If you really want to run it using npm script, then you can use the following configuration:

    {
      "type": "node",
      "request": "launch",
      "name": "Launch via NPM",
      "runtimeExecutable": "npm",
      "windows": {
        "runtimeExecutable": "npm.cmd"
      },
      "runtimeArgs": [
        "run-script",
        "dev"
      ],
      "port": 5858
    }
    

    but you also have to change your script command (specify a debug port)

      "scripts": {
        "dev": "node --nolazy --debug-brk=5858 tasks/runner.js"
      },
    

    You can explore various debug configurations simply by clicking the gear icon and selecting one.

    More about Node.js debugging can be found in the VS Code documentation.

    0 讨论(0)
提交回复
热议问题