如何在VSCode中调试NodeMon项目 [英] How to debug a nodemon project in VSCode
问题描述
我有一个NodeJs项目,我使用nodemon运行它,
我希望在调试模式下运行它来完成开发任务,但我无法这样做。
I have a NodeJs project and I run it using nodemon,
I wish to run it in debug mode for development tasks, but I am unable to do so.
我发现我需要在.vscode文件夹下的launch.json文件中添加正确的配置,
我有一个 app .js
文件,它是主应用程序文件。
然后该应用程序在节点版本4.6.2
和端口8080
。
通常情况下,我使用 npm run dev
命令运行应用程序。
I found that I'll need to add the right configuration to the launch.json file under .vscode folder,
I have a app.js
file which is the main app file.
And the application runs on node version 4.6.2
and on Port 8080
.
In usual case I run the App using npm run dev
command.
以下是我的launch.json文件-
{
// 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": "launch",
"name": "MyApp",
"program": "${workspaceFolder}/app.js",
"runtimeVersion": "4.6.2",
"protocol": "legacy",
"port": 8080
//"runtimeExecutable": "/home/user/.nvm/versions/node/v4.6.2/bin/node"
},
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceRoot}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"runtimeVersion": "4.6.2",
"protocol": "legacy",
"port": 8080
},
{
"type": "node",
"request": "launch",
"name": "DEBUG",
"runtimeExecutable": "nodemon",
"program": "${workspaceFolder}/app.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"runtimeVersion": "4.6.2",
"protocol": "legacy",
"port": 8080
}
]
}
package.json如下-
{
"name": "myapp",
"description": "myapp",
"version": "1.35.0",
"private": true,
"scripts": {
"dev": "nodemon app.js",
"debug": "nodemon app.js"
},
"dependencies": {
"async": "1.3.0",
"aws-sdk": "2.7.20",
"aws-xray-sdk": "^2.1.0",
"aws-xray-sdk-restify": "^1.3.0-beta",
"bcrypt": "0.8.5",
"body-parser": "1.12.3",
"compression": "^1.7.0",
"connect-flash": "0.1.1",
"cookie-parser": "1.3.4",
"cron": "1.0.9",
"csurf": "^1.9.0",
"csvtojson": "^1.1.2",
"date-utils": "1.2.16",
"dotenv": "4.0.0",
"email-templates": "1.2.1",
"express": "4.12.3",
"express-handlebars": "2.0.0",
"express-jwt": "^5.1.0",
"express-mailer": "0.2.4",
"express-session": "1.11.1",
"express-validator": "3.1.3",
"handlebars": "^3.0.3",
"helmet": "^3.5.0",
"html-pdf": "1.4.0",
"json-2-csv": "2.0.12",
"jsonwebtoken": "^7.3.0",
"multer": "^0.1.8",
"mysql": "2.6.2",
"newrelic": "1.25.0",
"node-schedule": "^1.3.0",
"nodemailer": "^1.3.4",
"nodemailer-ses-transport": "1.2.0",
"passport": "0.2.1",
"passport-local": "1.0.0",
"path": "0.11.14",
"promise": "7.0.0",
"qs": "^2.4.1",
"replaceall": "0.1.6",
"request": "2.55.0",
"run-parallel": "1.1.0",
"validator": "^7.0.0",
"winston": "^2.3.1",
"winston-daily-rotate-file": "^1.7.0",
"xlsx": "0.8.8"
},
"devDependencies": {
"nodemon": "^1.17.3"
}
}
我在运行DEBUG和nodemon配置时启动了该应用程序,
但是代码并没有在我放在app.js文件上的断点处暂停。
参考链接-
1. https:// github .com / Microsoft / vscode-recipes / tree / master / nodemon
2. https://github.com/bdspen/nodemon_vscode
3. 可以将Visual Studio Code配置为与nodemon一起启动
4. 无法通过附加到Chrome来在VSCode中进行调试
5。 https ://code.visualstudio.com/docs/editor/debugging
Reference links -
1. https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
2. https://github.com/bdspen/nodemon_vscode
3. Can Visual Studio Code be configured to launch with nodemon
4. Cannot debug in VSCode by attaching to Chrome
5. https://code.visualstudio.com/docs/editor/debugging
package.json或任何其他要求的更改更正了启动配置-launch.json,这将有助于我为用例调试VSCode中的应用程序吗?
What changes are required in package.json, or any corrections in Launch configuration - launch.json, that would help me to debug the application in VSCode for my usecase?
推荐答案
更改程序包.json到
Change package.json to
"scripts": {
"dev": "node app.js",
"debug": "nodemon --inspect app.js"
}
-检查是否版本> = 6.3。 --legacy或--auto 对于较旧的版本
然后将launch.json转换为:
And launch.json to:
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector"
}
]
重新启动标志是此处的关键。
the restart flag is the key here.
通过新的调试脚本启动应用程序
Start app via new debug script
npm运行调试
npm run debug
- 在调试视图中,选择 Node:Nodemon 配置,然后按play
或F5 - 选择从上方开始的过程
- In Debug view, select the Node: Nodemon configuration and press play or F5
- Choose the process started above
查看更多信息: vscode nodemon食谱
这篇关于如何在VSCode中调试NodeMon项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!