javascript – 如何在VSCode编辑器中调试NodeJS(ES6)代码?

我正在尝试从VSCode调试我在ES6中编写的nodejs应用程序.
但它抛出以下错误
node --debug-brk=18712 --nolazy index.js 
Debugger listening on [::]:18712
/Users/rsiva/Projects/Siva/ntask/ntask-api/index.js:1
(function (exports,require,module,__filename,__dirname) { import express from "express";
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

我看过How do I debug vue js application in VS Code?https://medium.com/@katopz/how-to-debug-es6-nodejs-with-vscode-8d00bd6c4f94#.yaevayjs3,但这些解决方案无效.

我的package.json:

{
  "name": "ntask-api","version": "1.0.0","description": "Task list API","main": "index.js","scripts": {
    "start": "babel-node index.js"
  },"author": "Siva","dependencies": {
    "babel-cli": "^6.5.1","babel-preset-es2015": "^6.5.0","consign": "^0.1.2","express": "^4.13.4","sequelize": "^3.19.2","sqlite3": "^3.1.8"
  },"devDependencies": {
    "babel-register": "^6.18.0"
  },"babel": {
    "presets": [
      "es2015"
    ],"sourceMaps": true,"retainLines": true
  }
}

launch.json:

{

    "version": "0.2.0","configurations": [
        {
            "type": "node","request": "launch","name": "Launch Program","program": "${workspaceRoot}/index.js","cwd": "${workspaceRoot}","sourceMaps": true
        },{
            "type": "node","request": "attach","name": "Attach to Process","port": 5858
        }
    ]
}

我知道我正在使用babel-node通常从控制台运行应用程序以使用ES6,但是如何让VSCode使用babel-node而不是node?

解决方法

您需要将launch.json配置文件中的runtimeExecutable设置为babel-node路径的值.
{
    "version": "0.2.0","name": "Launch via Babel","runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node","cwd": "${workspaceRoot}"
        }
    ]
}

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...