drm*_*wer 5 debugging node.js npm visual-studio-code
我正在尝试在 launch.json 中创建一个配置,该配置将npm test在文件所在的文件夹中运行.js。npm test在终端中手动运行效果很好,从scriptsmy 的部分获取相关命令package.json:
"scripts": {
"start": "node --experimental-json-modules nodeserver.js",
"test": "export MY_VAR=abc && node --experimental-json-modules nodeserver.js"
},
Run Code Online (Sandbox Code Playgroud)
特别是,当npm test直接在终端中运行时,test脚本行中指定的环境变量会生效,并且--experimental-json-modules标志会传递给node.
这是我的 launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这几乎是编辑器中建议的预定义选项之一的原样,并且与此非常相似。
但是当我在文件上运行此配置时nodeserver.js,我得到:
node它似乎在没有我在配置中指定的标志的情况下运行?我对这个计划的运作方式有什么误解launch.json?
编辑我玩得越多,配置似乎被完全忽略了,因此它使用默认的 node.js 配置...我从下拉列表中选择配置并按播放图标:
那应该有效吗?
除了npm start在终端中运行之外,使其工作的唯一“自动”方法是打开package.json并单击标签旁边出现的小调试按钮scripts:
但我想弄清楚如何launch.json正确使用,以便我可以通过它传递环境变量等。
小智 0
您可以尝试直接在 launch.json 中创建 npm 测试脚本,如下所示:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run npm test",
"request": "launch",
"type": "node",
"args": ["--experimental-json-modules", "${workspaceFolder}/nodeserver"],
"env": {
"MY_VAR": "abc"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)