index.htmlhtml
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> aaa <script src='main.js'></script> </body> </html>
main.js前端
var a = 1 var b = 2 console.log(b)
须要先安装插件Debugger for Chromenode
{ "name": "Launch index.html", "type": "chrome", "request": "launch", "sourceMaps": false, "file": "${workspaceRoot}/chrome/index.html" // 你的index.html地址 }
这里要选咱们刚刚建立的那个配置,即name字段webpack
能够看到,程序运行至断点处git
app.jsgithub
var a = 1 var b = 3 console.log(b)
{ "type": "node", "request": "launch", "name": "Launch node program", "program": "${workspaceRoot}/app.js", "runtimeExecutable": "node" },
注意: 若是程序报找不到node,则须要加上下面这句web
``` "runtimeVersion": "10.14.2", // 与你当前使用的node版本一致,若是你是使用nvm进行node版本管理,则须要加上这个,不然可能找不到node ``` 这种场景通常出如今:你使用nvm管理node,但没有全局安装node
通常咱们的项目命令都写在npm script里,咱们这里讲一些怎么跑这些scriptchrome
接上一节,咱们再建立一个package.json,注意里面的scriptstypescript
注意9229这个端口npm
{ "name": "npm", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "debugger": "node --nolazy --inspect-brk=9229 app.js" }, "keywords": [], "author": "", "license": "ISC" }
{ "type": "node", "request": "launch", "name": "Launch via NPM", "runtimeVersion": "10.14.2", "runtimeExecutable": "npm", "runtimeArgs": [ "run-script", "debugger" // 需跟package里定义的一致 ], "port": 9229 // 需与package里定义的inspect-brk一致 },
接上一节
{ "type": "node", "request": "launch", "name": "nodemon", "runtimeVersion": "10.14.2", "runtimeExecutable": "nodemon", "args": ["${workspaceRoot}/app.js"], "restart": true, "protocol": "inspector", //至关于--inspect了 "sourceMaps": true, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", },
这时咱们修改b变量就能从新刷新了
rollup相似,这对于调试源码很是有用
这里咱们以app.js为入口文件
var b =1 console.log(b) module.exports = { entry: './app.js', };
cnpm i webpack webpack-cli -S
"webpack": "webpack", "build": "node --inspect-brk=9230 ./node_modules/.bin/webpack --config webpack.config.js",
注意:咱们要用./node_modules/.bin/webpack来启动服务
{ "type": "node", "runtimeVersion": "10.14.2", "request": "launch", "name": "webpack debugger", "runtimeExecutable": "npm", "runtimeArgs": ["run", "build"], "port": 9230 },
因为ts文件不能直接运行和调试,咱们须要先将其转为js再进行调试
cnpm i typescript ts-node -S
其中ts-node能够直接用来执行ts文件
index.ts
const t: number = 0; console.log(t)
tsconfig.json
{ "compilerOptions": { "target": "es5", "sourceMap": true }, "include": ["."] }
{ "type": "node", "request": "launch", "name": "ts debugger", "runtimeVersion": "10.14.2", "runtimeExecutable": "node", "runtimeArgs": [ "-r", "ts-node/register" ], "args": [ "${workspaceFolder}/index.ts" ] },
这里的意思是经过 node 来启动 /src/index.ts,在启动时为 node 注入一个 ts-node/register 模块,以即可以执行 ts 类型的文件
以上代码能够在 https://github.com/repototest/vscode-debugger-demo 找到
更多优秀项目
https://www.barretlee.com/blog/2019/03/18/debugging-in-vscode-tutorial/
https://www.jianshu.com/p/88d9a1e6fdcd