[英]How to debug remote node.js app using chrome devtools
I have a simple console node.js app that is running on a remote server.我有一个在远程服务器上运行的简单控制台 node.js 应用程序。 I would like to debug it remotely using the Chrome DevTools.
我想使用 Chrome DevTools 远程调试它。 How can I do that?
我怎样才能做到这一点?
Follow this instruction here Another good article here按照此说明这里的另一个很好的文章在这里
Let's say you are running Node on remote machine, remote.example.com, that you want to be able to debug.假设您在远程机器 remote.example.com 上运行 Node,您希望能够调试该机器。 On that machine, you should start the node process with the inspector listening only to localhost (the default).
在该机器上,您应该使用检查器仅侦听 localhost(默认设置)来启动节点进程。
$ node --inspect server.js
Now, on your local machine from where you want to initiate a debug client connection, you can setup an ssh tunnel:现在,在您想要启动调试客户端连接的本地计算机上,您可以设置 ssh 隧道:
$ ssh -L 9221:localhost:9229 user@remote.example.com
Then on your local machine in the Chrome browser go to this address:然后在本地机器上的 Chrome 浏览器中转到这个地址:
chrome://inspect/#devices
铬://检查/#devices
You should see something like this:您应该会看到如下内容:
Once you click inspect
you should see the familiar Chrome developers tool window.单击
inspect
您应该会看到熟悉的 Chrome 开发人员工具窗口。 Good luck!祝你好运!
on remote server:在远程服务器上:
$ node --inspect server.js
$ node --inspect server.js
On your local use Putty to create ssh tunel.在本地使用Putty创建 ssh tunel。
Click On Add botton:点击添加按钮:
On session tab Click to save!在会话选项卡上单击以保存!
And Click on Open.然后点击打开。
You can check Tunnel is open with the following command:您可以使用以下命令检查隧道是否打开:
netstat -a -n |
netstat -a -n | grep 9221
grep 9221
On Your local open Chrome navigate to:在您本地打开的 Chrome 上导航到:
chrome://inspect/#devices
铬://检查/#devices
With Windows Client and Visual Studio Code使用 Windows 客户端和 Visual Studio Code
Here's a variation of Peter's answer, using Visual Studio Code instead of Chrome.这是 Peter 答案的变体,使用 Visual Studio Code 而不是 Chrome。
remote
to local
as pointed out in the first comment to @Peter's answer).remote
更改为local
如@Peter 回答的第一条评论中指出的那样)。{
// 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": [
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "127.0.0.1",
"port": 9221,
"localRoot": "${workspaceFolder}/npm",
"remoteRoot": "/abs/path/npm",
"skipFiles": [
"<node_internals>/**"
]
},
]
}
inpect-brk
to pause your program until the Visual Studio Code has connected to the remote host:inpect-brk
暂停程序,直到 Visual Studio Code 连接到远程主机:node --inspect-brk your-node-program.js
There are security considerations, but the following can be an easy/safe solution if on the same network:有安全考虑,但如果在同一网络上,以下可能是一个简单/安全的解决方案:
On server, launch your app:在服务器上,启动您的应用程序:
node --inspect=0.0.0.0:9229 server.js
Rather than having node bind strictly to localhost, you can have it bind to any network interface via 0.0.0.0:9229
.您可以通过
0.0.0.0:9229
将节点绑定到任何网络接口,而不是让节点严格绑定到 localhost。
Now on your PC, open chrome and goto chrome://inspect/#devices
.现在在您的 PC 上,打开 chrome 并转到
chrome://inspect/#devices
。 Chrome has changed the UI accouple times but you should be able to configure your remote server IP as a target. Chrome 已更改 UI accouple 时间,但您应该能够将远程服务器 IP 配置为目标。 Eg
[server_ip]:9229
.例如
[server_ip]:9229
。 Note, this would be safest to use on a local network where server_ip
is a local IP address.请注意,这在
server_ip
是本地 IP 地址的本地网络上使用是最安全的。 If you are debugging against a public IP address there would be a risk of someone else attaching.如果您针对公共 IP 地址进行调试,则存在其他人附加的风险。
You can follow the steps你可以按照步骤
1)Run the application in remote
2)Open application in chrome
3)Open Developer Tools--->Sources
4)Ctrl + p
5)Open file you want to debug there<filename.js>
6)Place debug points in your now opened file.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.