VS Code 断点调试集成webpack与nodemon插件的Express项目时出现未绑定断点问题
Express.js 开发模板调试功能配置(断点未绑定问题场景)
正在搭建Express.js项目通用开发模板,配置项目调试功能时始终出现断点未绑定问题,以下为梳理的全部相关配置资料:
梳理的资料范围
- 项目依赖与脚本配置
package.json - 项目打包配置
webpack.config.js - VS Code 调试配置
launch.json - 项目目录结构
- 断点未绑定错误提示
- 控制台运行日志
各部分配置详情
package.json 配置
{ "dependencies": { "bcryptjs": "^2.4.3", "body-parser": "^1.19.0", "cookie-parser": "^1.4.5", "cors": "^2.8.5", "crypto": "^1.0.1", "debug": "~2.6.9", "ejs": "^3.1.5", "express": "~4.16.1", "express-jwt": "^6.0.0", "joi": "^17.3.0", "joi-password-complexity": "^5.0.2", "jsonwebtoken": "^8.5.1", "mailgun.js": "^2.0.6", "mongodb": "^3.6.3", "mongoose": "^5.11.11", "mongoose-paginate-v2": "^1.3.13", "morgan": "^1.10.0", "mustache": "^4.1.0", "nodemailer": "^6.4.17", "path": "^0.12.7", "querystring": "^0.2.0", "saslprep": "^1.0.3", "swagger-ui-express": "^4.1.6", "winston": "^3.3.3", "winston-daily-rotate-file": "^4.5.0" }, "devDependencies": { "@babel/core": "^7.12.10", "@babel/plugin-proposal-class-properties": "^7.12.1", "@babel/plugin-proposal-decorators": "^7.12.12", "@babel/plugin-proposal-do-expressions": "^7.12.1", "@babel/plugin-proposal-export-default-from": "^7.12.1", "@babel/plugin-proposal-export-namespace-from": "^7.12.1", "@babel/plugin-proposal-function-bind": "^7.12.1", "@babel/plugin-proposal-function-sent": "^7.12.1", "@babel/plugin-proposal-json-strings": "^7.12.1", "@babel/plugin-proposal-logical-assignment-operators": "^7.12.1", "@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.1", "@babel/plugin-proposal-numeric-separator": "^7.12.7", "@babel/plugin-proposal-optional-chaining": "^7.12.7", "@babel/plugin-proposal-pipeline-operator": "^7.12.1", "@babel/plugin-proposal-throw-expressions": "^7.12.1", "@babel/plugin-syntax-dynamic-import": "^7.8.3", "@babel/plugin-syntax-import-meta": "^7.10.4", "@babel/preset-env": "^7.12.11", "babel-cli": "^6.26.0", "babel-loader": "^8.2.2", "babel-preset-env": "^1.7.0", "copy-webpack-plugin": "^7.0.0", "eslint": "^7.17.0", "eslint-config-airbnb-base": "^14.2.1", "eslint-config-prettier": "^7.1.0", "eslint-import-resolver-webpack": "^0.13.0", "eslint-plugin-import": "^2.22.1", "eslint-plugin-ordered-imports": "^0.4.0", "eslint-plugin-prettier": "^3.3.1", "nodemon": "^2.0.7", "nodemon-webpack-plugin": "^4.3.2", "prettier": "^2.2.1", "webpack": "^5.13.0", "webpack-cli": "^4.3.1" }, "name": "web-api-template", "scripts": { "build": "npx webpack --mode production", "start-production": "npx webpack -w --mode production", "start": "npx webpack -w --mode development" }, "version": "0.0.0" }
webpack.config.js 配置
const CopyWebpackPlugin = require('copy-webpack-plugin'); const NodemonPlugin = require('nodemon-webpack-plugin'); const path = require('path'); const webpack = require('webpack'); module.exports = { entry: './src/app.js', externals: { saslprep: "require('saslprep')", }, module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader', }, }, ], }, output: { filename: 'app.js', path: path.resolve(__dirname, 'dist'), }, plugins: [ new CopyWebpackPlugin({ patterns: [ './node_modules/swagger-ui-dist/swagger-ui.css', './node_modules/swagger-ui-dist/swagger-ui-bundle.js', './node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js', './node_modules/swagger-ui-dist/favicon-16x16.png', './node_modules/swagger-ui-dist/favicon-32x32.png', ], }), new webpack.SourceMapDevToolPlugin({ filename: '[name].js.map', }), new NodemonPlugin({ nodeArgs: ['--inspect=9229'], }), ], resolve: { modules: [path.resolve(__dirname, './src'), 'node_modules'], extensions: ['.js', '.jsx', '.json'], }, target: 'node', };
VS Code 调试配置 launch.json
{ "configurations": [ { "args": [], "console": "integratedTerminal", "cwd": "${workspaceFolder}", "env": { "NODE_ENV": "development" }, "name": "Launch Program", "port": 9229, "preLaunchTask": null, "request": "launch", "runtimeArgs": [ "run-script", "start" ], "runtimeExecutable": "npm", "stopOnEntry": false, "type": "node", "sourceMaps": true, }, ], "version": "0.2.0" }
项目目录结构说明
项目根目录存放各类配置文件,src目录为源码存放入口,核心业务代码全部放在src路径下;webpack打包后将生成的输出文件写入dist目录,nodemon进程监听dist目录内的文件变动自动重启服务。
断点未绑定错误现象
在src目录的源码文件中手动设置的断点,在VS Code调试界面显示为灰色未激活状态,提示断点未绑定,代码执行到对应位置时无法触发暂停。
控制台运行输出日志
webpack 5.13.0 compiled with 13 warnings in 8376 ms [nodemon] 2.0.4 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): dist/app.js [nodemon] watching extensions: js,mjs,json [nodemon] starting `node --inspect=9229 dist/app.js` Debugger attached. 2021-02-04 13:36:33 info: Server running on port 4000
内容的提问来源于stack exchange,提问作者juliushuck
相关产品推荐
相关产品推荐

