如何调试使用 Chrome/WebKit 作为远程调试器运行的 Node.js 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12440169/
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
How do you debug a Node.js server running with Chrome/WebKit as the remote debugger?
提问by Chris G.
If you have your Node running
如果您的节点正在运行
node --debug server.js
This gives me a port number xxxx, should I use this port number when starting Chrome?
这给了我一个端口号 xxxx,我应该在启动 Chrome 时使用这个端口号吗?
Do you remote debug into it from Google\ Chrome --remote-debugging-port=xxxx?
你从远程调试到它Google\ Chrome --remote-debugging-port=xxxx吗?
Or is the 9222 a magic port, as it is mentioned all over.
或者 9222 是一个神奇的端口,正如到处提到的那样。
Am I on the right track, trying to start Chrome with --remote-debuggerinto the Node.js server.js
我是否在正确的轨道上,尝试将 Chrome 启动--remote-debugger到 Node.jsserver.js
采纳答案by anneb
The node-inspector / --debug are now replaced by inspector See update below
node-inspector / --debug 现在被inspector 取代 请参阅下面的更新
#now deprecated / see below for update
#install node-inspector
npm install -g node-inspector
#start node-inspector, listen on port 8080 (default)
node-inspector --web-port=8080
#in another terminal session/window:
#while node-inspector is running, start your project in debug mode
node --debug myproject.js
Now you can browse to http://your_server:8080for a full debug session of myproject.js
现在您可以浏览到http://your_server:8080以查看 myproject.js 的完整调试会话
If your remote server is not accessible on the remote port because of firewalls or other reasons, you could create an ssh-tunnel to it from port 8080 on your local machine to 'localhost:8080' on the remote server:
如果由于防火墙或其他原因无法在远程端口上访问远程服务器,您可以创建一个 ssh 隧道,从本地计算机上的端口 8080 到远程服务器上的“localhost:8080”:
ssh -L 8080:localhost:8080 username@remoteserver -N
and keep this running while you use http://localhost:8080on your local machine to debug your remote nodejs session
并在本地机器上使用http://localhost:8080调试远程 nodejs 会话时保持运行
Update august 2017
2017 年 8 月更新
Start node in inspect mode:
以检查模式启动节点:
node --inspect=0.0.0.0:9229 myproject.js
or if you want the debugger to break at the first line of myproject.js:
或者如果您希望调试器在 myproject.js 的第一行中断:
node --inspect-brk=0.0.0.0:9229 myproject.js
Then open the following URL in your chrome browser:
然后在 Chrome 浏览器中打开以下 URL:
chrome://inspect
Click the 'Configure...' button and add the following target:
单击“配置...”按钮并添加以下目标:
ip-or-name-of-server-running-node:9229
After you click the 'Done' button, you should see myproject.js under your remote targets. Click the inspect link to start debugging. Unfortunately, the inspect link does not work on Chrome 58 for Ubuntu. It works fine on Chrome 60 for Windows.
单击“完成”按钮后,您应该会在远程目标下看到 myproject.js。单击检查链接开始调试。不幸的是,检查链接在 Ubuntu 的 Chrome 58 上不起作用。它在 Windows 版 Chrome 60 上运行良好。
回答by JohnnyHK
Use node-inspectorto remotely debug your node application from Chrome that you've started with the --debugoption as you've shown.
用于node-inspector从 Chrome 远程调试您的节点应用程序,您已使用--debug您显示的选项启动该应用程序。
回答by schmod
Recent versions of Node (> v6.3.0) and Chrome now allow you to use the Chrome Developer Tools to debug a Node.JS processwithout having to install anything else. Just pass --inspectto node:
最新版本的 Node (> v6.3.0) 和 Chrome 现在允许您使用 Chrome 开发人员工具来调试 Node.JS 进程,而无需安装任何其他东西。只需传递--inspect给node:
$ node --inspect script.js
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://SOME-URL-HERE
Just open that URL in Chrome, and you're good to go.
只需在 Chrome 中打开该 URL,您就可以开始使用了。
If you need to pause your script immediately after Node starts, you can also pass --debug-brkin the same command.
如果你需要在 Node 启动后立即暂停你的脚本,你也可以传入--debug-brk相同的命令。
回答by hushenglang
using $ vagrant ssh -- -L 5858:127.0.0.1:5858
to ssh connect to VM. also this comment would start a proxy server on port 5858;you could test using telnet 127.0.0.1 5858 to see if local proxy server started or not.
In VM, you can start node with command
$ node --debug-brk app.js
- set up debug configuration in web storm.
- when you start debug in web storm, node.js server in VM will start in a few seconds.
使用 $ vagrant ssh -- -L 5858:127.0.0.1:5858
to ssh connect to VM. also this comment would start a proxy server on port 5858;您可以使用 telnet 127.0.0.1 5858 进行测试以查看本地代理服务器是否已启动。
在VM中,您可以使用命令启动节点
$ node --debug-brk app.js
- 在 webstorm 中设置调试配置。
- 当你在 webstorm 中启动 debug 时,VM 中的 node.js 服务器将在几秒钟内启动。
PS: there is no need to touch the vagrant file. Reference: Connecting WebStorm to a remote node.js debugging session.
PS:没有必要去碰vagrant文件。参考:将 WebStorm 连接到远程 node.js 调试会话。

