我在我的系统中安装了nodemon
作为全局包。它在cmd中执行nodemon
时有效。
但是当我在这个launch.json
文件中使用vscode时,vscode会抛出此异常:
请求启动:运行时可执行文件XXX \ XXX \ XXX \ XXX \ nodemon不存在
launch.json是:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "app.js",
"stopOnEntry": false,
"args": [],
"cwd": ".",
"runtimeExecutable": nodemon,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"preLaunchTask": "",
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
当我在runtimeExecutable
中擦除nodemin时,它与节点完美地运行
首先,将nodemon安装为dev依赖项:
npm install --save-dev nodemon
对于较新版本的VS Code,请设置.vscode/launch.json
文件,如下所示:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}]
}
最重要的部分是指向nodemon脚本的runtimeExecutable
属性和指向入口点脚本的program
属性。
如果您使用较旧的VS代码(您不应该使用),请尝试以下启动配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch with nodemon",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/nodemon/bin/nodemon.js",
"args": ["${workspaceRoot}/app.js"],
"runtimeArgs": ["--nolazy"]
}
]
}
最重要的部分是指向nodemon脚本的program
属性和指向正常入口点脚本的args
属性。
我无法得到@AdrianT的答案,附带调试器。看起来有一种更新的内置支持方式来执行此操作:
它会在你的launch.json中添加这样的东西:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceRoot}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
确保您的“程序”设置是正确的入口点脚本。
您需要全局安装nodemon才能使其正常工作(npm install -g nodemon)(根据the documentation)
您的应用程序现在运行,您可以设置将被命中的断点,控制台将记录到集成终端窗口。
请注意,终止调试会话只会终止要调试的程序,而不是nodemon本身。要终止nodemon,请在集成终端中按Control-C。
在Visual Studio代码中创建启动配置:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"restart": true
}
从命令行运行nodemon:nodemon --debug server.js
现在从VC和vuala'附加'。
附加绝对是一个简单的选择。为了确保代码中断,请确保使用--inspect-brk
(节点8+)运行nodemon,例如:
nodemon --inspect-brk src/app.js
启动nodemon后,将打开端口以进行调试连接:
Debugger listening on ws://127.0.0.1:9229/someUUID
您可以使用该端口来构建启动配置,这非常简单:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"restart": true
},
不,目前它不能。但是我设法使用nodemon来解决这个问题。我是从Grunt开始的。但是等效的命令行也应该这样做。
编辑:经过一个晚上的测试后,我可以说下面的方法仍然有点不稳定:S,间接失败,有时断点被忽略。
编辑2:您还可以使用['--debug-brk=5860']
为nodeArgs
在Gruntfile中指定非默认调试端口。我也被建议使用--debug-brk
而不是--debug
。也许这将消除目前的诡异。我会回来提一下它是否有帮助(我目前正在切换项目)。
如果这可能有助于任何人在Windows 10上使用当前VS代码版本中的以下设置(例如v0.10.6),但它也可能在Mac上工作(我可能稍后再查看)。但请注意,我有时必须通过在调试器选择之前更改+保存文件来触发重建。
/.vscode/launch.json
{
"configurations": [{
"name": "Launch",
"outDir": null
},{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}]
}
/Gruntfile.js
nodemon : {
dev : {
script : 'launcher.js'
},
options : {
ignore : ['node_modules/**', 'Gruntfile.js'],
nodeArgs: ['--debug'],
env : { PORT : '4123'
}
}
}
我想调试端口5858是默认值,因为它没有在这里指定(请注意它在上面的launch.json
中。)
是的你可以!从最近的更新开始,您可以将调试器附加到正在运行的Nodemon进程。 This page has more information。在页面上搜索nodemon以查看说明。
我使用Node Exec插件。它允许您通过按F8和F9(在编辑器中的打开文件上应用)在vcs中运行和停止节点应用程序。这可能有助于(临时)解决方法。
https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
以上链接帮助我成功调试了nodemon + express app。这些步骤很好地解释了。
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector",
}
]
}
npm脚本
"dev-server": "nodemon ***--inspect*** server.js"
脚步:
现在你应该能够调试了。
如果它没有帮助你,那么请看看官方文档,配置选项在那里解释。 https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools