node.js 可以将 Visual Studio Code 配置为使用 nodemon 启动吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34450175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Visual Studio Code be configured to launch with nodemon
提问by Mickel Sierra
I have installed nodemonas a global package in my system.
It works when I executed nodemonin cmd.
我已nodemon在我的系统中作为全局包安装。当我nodemon在 cmd 中执行时它有效。
But when I am using vscode with this launch.jsonfile, vscode throws this exception:
但是当我在这个launch.json文件中使用 vscode 时,vscode 抛出这个异常:
request launch: runtime executable XXX\XXX\XXX\XXX\nodemon does not exists
请求启动:运行时可执行文件 XXX\XXX\XXX\XXX\nodemon 不存在
the launch.json is:
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
}
]
}
when I erase the nodemin in runtimeExecutableit runs perfectly with node
当我擦除其中的 nodemin 时,runtimeExecutable它与 node 完美运行
回答by Adrian T
First, install nodemon as a dev dependency:
首先,将 nodemon 安装为开发依赖项:
npm install --save-dev nodemon
For newer versions of VS Codeset up your .vscode/launch.jsonfile like this:
对于较新版本的 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"
}]
}
The most important pieces are the runtimeExecutableproperty that points to the nodemon script and the programproperty that points to your entry point script.
最重要的部分是runtimeExecutable指向 nodemon 脚本的program属性和指向入口点脚本的属性。
If you use an older VS Code(which you shouldn't), try this launch configuration:
如果您使用较旧的 VS Code(您不应该使用),请尝试以下启动配置:
{
"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"]
}
]
}
The most important pieces are the programproperty that points to the nodemon script and the argsproperty that points to your normal entry point script.
最重要的部分是program指向 nodemon 脚本的args属性和指向普通入口点脚本的属性。
回答by Mathew
I couldn't get @AdrianT's answer working with the debugger attached. It seems like there's a newer built-in supported way to do this:
我无法使用附加的调试器获得@AdrianT 的答案。似乎有一种更新的内置支持方法可以做到这一点:
- Open the Launch Configuration dropdown and select "Add configuration..."
- Select "Node.js: Nodemon Setup"
- 打开启动配置下拉菜单并选择“添加配置...”
- 选择“Node.js:Nodemon 设置”
It will add something like this to your launch.json:
它将在您的 launch.json 中添加如下内容:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceRoot}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
Make sure your "program" setting is your correct entry point script.
确保您的“程序”设置是正确的入口点脚本。
You need to install nodemon globally to get this to work (npm install -g nodemon) (as per the documentation)
您需要全局安装 nodemon 才能使其正常工作(npm install -g nodemon)(根据文档)
Your app now runs and you can set breakpoints which will be hit and the console logs to the integrated terminal window.
您的应用程序现在可以运行,您可以设置将被命中的断点,并且控制台会记录到集成的终端窗口。
Note that terminating the debug session only terminates the program to debug, not nodemon itself. To terminate nodemon, press Control-C in the integrated terminal.
请注意,终止调试会话只会终止要调试的程序,而不是 nodemon 本身。要终止 nodemon,请在集成终端中按 Control-C。
回答by Yevgeni
In Visual studio code create a launch config:
在 Visual Studio 代码中创建启动配置:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"restart": true
}
run nodemon from the command line: nodemon --debug server.js
从命令行运行 nodemon: nodemon --debug server.js
Now 'Attach' from VC and vuala.
现在“附加”来自 VC 和 vuala。
回答by Fred
Attaching is definitely an easy option. In order to make sure that your code breaks, make sure you run nodemon with --inspect-brk(node 8+), e.g.:
附加绝对是一个简单的选择。为了确保您的代码中断,请确保您使用--inspect-brk(节点 8+)运行 nodemon ,例如:
nodemon --inspect-brk src/app.js
After launching nodemon will log the port open for debug connections:
启动 nodemon 后,将记录为调试连接打开的端口:
Debugger listening on ws://127.0.0.1:9229/someUUID
You can take that port in order to build your launch config which is quite simple:
您可以使用该端口来构建您的启动配置,这非常简单:
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"restart": true
},
回答by Bart
No, currently it can't. But I managed to get this somewhat working using nodemon. I start it from Grunt . But an equivalent command line should do the same.
不,目前不能。但是我设法使用 nodemon 使这有点工作。我从 Grunt 开始。但是等效的命令行应该做同样的事情。
EDIT: After an evening of testing I can say that below approach is still somewhat flakey :S, attaching fails intermittedly and sometimes breakpoints are ignored.
编辑:经过一个晚上的测试,我可以说下面的方法仍然有些古怪:S,连接间歇性失败,有时会忽略断点。
EDIT2: You can also specify an non default debug port in Gruntfile using ['--debug-brk=5860']for nodeArgs. I've been also advised to use --debug-brkinstead of --debug. Perhaps this will remove the current flakeyness. I'll come back and mention here if it helps (I've currently switched project).
EDIT2:您还可以在 Gruntfile 中使用['--debug-brk=5860']for指定非默认调试端口nodeArgs。我还被建议使用--debug-brk而不是--debug. 也许这会消除当前的脆弱性。如果有帮助,我会回来并在这里提及(我目前已切换项目)。
In case this might help anyone it's working with below settings in Current VS Code version (e.g. v0.10.6) on Windows 10. But it'll probably work on Mac too (I might check later). But note that I sometimes have to trigger a rebuild by changing+saving a file before the debugger picks it up.
如果这可能对在 Windows 10 上使用当前 VS Code 版本(例如 v0.10.6)中的以下设置有所帮助的任何人有所帮助。但它可能也适用于 Mac(我可能会稍后检查)。但请注意,有时我必须在调试器获取文件之前通过更改+保存文件来触发重建。
/.vscode/launch.json
/.vscode/launch.json
{
"configurations": [{
"name": "Launch",
"outDir": null
},{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}]
}
}
/Gruntfile.js
/Gruntfile.js
nodemon : {
dev : {
script : 'launcher.js'
},
options : {
ignore : ['node_modules/**', 'Gruntfile.js'],
nodeArgs: ['--debug'],
env : { PORT : '4123'
}
}
}
I guess the debug port 5858 is the default since it's not specified here (note it ís in launch.jsonabove.)
我猜调试端口 5858 是默认端口,因为这里没有指定它(注意它在launch.json上面。)
回答by Aron
Yes you can! As of a recent update you can attach the debugger to a running Nodemon process. This page has more information. Search for nodemon on the page to see the instructions.
是的你可以!从最近的更新开始,您可以将调试器附加到正在运行的 Nodemon 进程。此页面有更多信息。在页面上搜索 nodemon 以查看说明。
回答by Ninjaneer
https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
The above link helped me to successfully debug nodemon + express app. The steps are well explained there.
上面的链接帮助我成功调试了 nodemon + express 应用程序。这些步骤在那里得到了很好的解释。
launch.json
启动文件
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector",
}
]
}
}
npm script
npm 脚本
"dev-server": "nodemon ***--inspect*** server.js"
Steps:
脚步:
- Run the server, using npm script. Please note --inspect arg in the script
- Start visual code debugger, A prompt will be shown to select the node server process
- select the node server process
- 使用 npm 脚本运行服务器。请注意脚本中的 --inspect arg
- 启动可视化代码调试器,会提示选择节点服务器进程
- 选择节点服务器进程
Now you should be able to debug.
现在您应该可以调试了。
if it did not help you, then please have a look at the official doc, the config options are explained there. https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools
如果它没有帮助您,那么请查看官方文档,那里解释了配置选项。 https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools
回答by KiSa87
I use the Node Exec plugin. It allows you to run and stop a node app in vcs by pressing F8 and F9 (applies on open file in editor). This could help as a (temporary) workaround.
我使用 Node Exec 插件。它允许您通过按 F8 和 F9(适用于编辑器中打开的文件)在 vcs 中运行和停止节点应用程序。这可以作为(临时)解决方法提供帮助。

