1、修改 :.vscode/launch.json
我用的是 express ,还有就是一定不要调试正式环境!!!!!
{
// Use IntelliSense to learn about possible Node.js debug 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": "launch",
"name": "调试本地",
"program": "${workspaceRoot}/bin/www",
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "attach",
"name": "调试服务器的dev", // 启动项的别名
"address": "81.120.110.175", // 远程服务器的IP地址
"port": 9229, // 远程nodejs服务断点服务的端口,跟下面 2中 的 9229相对应
"localRoot": "${workspaceFolder}",
"remoteRoot": "/var/www/github/dev/nodejs-demo-api" // 这里是你服务器上的项目根目录
}
]
}
注意 9229 端口需要开放
2、服务端启动项目
进入到你的项目根目录,执行命令:
node --inspect=81.120.110.175:9229 bin/www
注意:这里是前台启动,所以不要关闭命令行对话框。 还有就是如果你已经用pm2启动该服务了,那么得先停掉,否则会提示端口号被占用。
3、vscode中调试服务端项目
打开vscode 的调试栏,选择 调试服务器的dev
调试项,然后点击左侧的绿色三角号
image.png
image.png
不出意外的话vscode的 调试控制台
会输出相应的启动信息,此时找到你想断点的文件中的位置,直接断点即可。
一定要保证本地代码和服务端的代码的一致性