在我的package.json
中,我定义了一些脚本,如下所示:
"scripts": {
"build": "tsc -p tsconfig.json",
"run": "node --experimental-specifier-resolution=node .",
"start": "npm run build && npm run run"
}
现在,我只是使用终端上的npm run start
编译和运行所有的东西,但是我现在想使用断点,并且希望切换到VSCode调试。
我不知道运行脚本的launch.json
配置应该是什么样子。
我的项目结构如下所示:
.
├── package.json
├── src/
│ └── start.ts
└── dist/
└── start.js
我认为到目前为止我最好的尝试是:
{
"name": "Launch via NPM",
"request": "launch",
"type": "node",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"start"
],
},
不幸的是,这给了我以下错误:
Exception has occurred: Error: ENOENT: no such file or directory, stat '{cwd}\git.COM'
用"runtimeArgs": ["run","start"],
替换"command": "npm run start",
也会导致同样的错误。
发布于 2022-04-23 13:14:54
使用NPM脚本
您可以在您的package.json
中创建一个附加脚本,以启动节点,并使用等待附加调试器的指令。IMHO,这并不理想,我会避免它,但有时它是必要的(例如,当节点由某个shell脚本启动时):
"scripts": {
"debug": "npm run build && node --experimental-specifier-resolution=node --inspect-brk ."
}
然后,您需要在launch.json
中进行配置,以便将调试器附加到等待节点进程:
{
"name": "Attach",
"type": "node",
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
}
直接发射节点
另一种可能是使用适当的参数启动launch.json
中的节点。您的package.json有一些代码复制,但这是我自己做的。
请注意,如果要直接调试TS文件,则必须生成源映射并指示生成的JS文件的位置。
以下是它的样子:
{
"name": "Debug",
"type": "node",
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/src/start.ts",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"runtimeArgs": [
"--experimental-specifier-resolution=node"
]
}
为了确保构建了TS代码,我将在另一个终端窗口中以监视模式运行TSC:
tsc -p tsconfig.json --watch
https://stackoverflow.com/questions/71978387
复制相似问题