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 Chromevue
{
"name": "Launch index.html",
"type": "chrome",
"request": "launch",
"sourceMaps": false,
"file": "${workspaceRoot}/chrome/index.html" // 你的index.html地址
}
複製代碼
這裏要選咱們剛剛建立的那個配置,即name字段node
能夠看到,程序運行至斷點處webpack
app.jsgit
var a = 1
var b = 3
console.log(b)
複製代碼
{
"type": "node",
"request": "launch",
"name": "Launch node program",
"program": "${workspaceRoot}/app.js",
"runtimeExecutable": "node"
},
複製代碼
注意: 若是程序報找不到node,則須要加上下面這句github
```
"runtimeVersion": "10.14.2", // 與你當前使用的node版本一致,若是你是使用nvm進行node版本管理,則須要加上這個,不然可能找不到node
```
這種場景通常出如今:你使用nvm管理node,但沒有全局安裝node
複製代碼
通常咱們的項目命令都寫在npm script裏,咱們這裏講一些怎麼跑這些scriptweb
接上一節,咱們再建立一個package.json,注意裏面的scriptschrome
注意9229這個端口typescript
{
"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 類型的文件
能夠直接參考官網在 VS Code 中調試
以上代碼能夠在 github.com/repototest/… 找到
更多優秀項目