【问题标题】:How to debug NodeJS(ES6) code in VSCode editor?如何在 VSCode 编辑器中调试 NodeJS(ES6) 代码?
【发布时间】:2017-06-16 08:26:50
【问题描述】:

我正在尝试从 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 而不是 节点?

【问题讨论】:

    标签: javascript node.js visual-studio-code babeljs


    【解决方案1】:

    需要将launch.json配置文件中的runtimeExecutable设置为babel-node路径的值。

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Launch via Babel",
                "program": "${workspaceRoot}/index.js",
                "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node",
                "cwd": "${workspaceRoot}"
            }
        ]
    }
    

    【讨论】:

    • 注意:在 Windows 上,请确保指定具有正确扩展名的可执行文件,例如使用 npm.cmd 而不是仅存在但是 Linux 和 macOS 的 shell 脚本的 npm。
    • 由于您可能最终需要安装可执行文件,我想指出该包名为babel-cli。并且建议将其安装在本地。 babeljs.io/docs/usage/cli/#installation
    • 虽然这个配置看起来很有希望,但它仍然会在导入语法上产生与 ES6 相关的错误,但我对如何利用 VS Code 的调试器感到非常困惑...i.imgur.com/sSj8Cr0.png
    • 在遵循@WaqasNoor 和@Felicio 的答案后,它对我有用。 Windows 用户须知:必须在路径中使用转义的反斜杠,使其变为"runtimeExecutable": "${workspaceRoot}\\node_modules\\.bin\\babel-node"
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2020-02-29
    相关资源
    最近更新 更多