一般情況下虹脯,我們會在node項(xiàng)目中使用babel淑仆,以便充分的使用最新的JS語法規(guī)范漆诽,以及提前使用還在草案階段的語法亭畜。但是當(dāng)我們使用babel之后扮休,我們在開發(fā)調(diào)試的時(shí)候一般會使用 babel-node 來代替node指令,使用babel-node之后進(jìn)行調(diào)試贱案,與直接使用node會有一些細(xì)微的區(qū)別肛炮,本文介紹如何使用VSCode調(diào)試使用babel-node啟動的程序。
在package.json的scripts中宝踪,添加debug指令侨糟。
"scripts": {
debug": "babel-node src/main --inspect --debug-brk --nolazy src/main"
}
在 項(xiàng)目目錄下,新建.vscode文件夾瘩燥,創(chuàng)建launch.js文件秕重,內(nèi)容如下:
{
// 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": "Launch-vai-babel-node",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"debug"
],
"port":9229,
"env": {
"NODE_ENV": "develop"
},
"console": "internalConsole"
}
]
}
至此配置基本完成。
切換到VSCode的debug標(biāo)簽厉膀,運(yùn)行就可以了溶耘。
image.png
然后就可以實(shí)現(xiàn)基本的斷點(diǎn)和單步調(diào)試等功能了。