【问题标题】:Running babel-node in visual studio code在 Visual Studio 代码中运行 babel-node
【发布时间】:2016-03-13 20:49:03
【问题描述】:

通常通过命令行启动,我可以输入:

babel-node server.js

当我尝试设置它以便断点和在我收到的 Visual Studio 代码中不起作用的内容时:

/Users/me/proj/node_modules/babel-cli/lib/babel-node.js --debug-brk=31893 --nolazy server.js 
/Users/me/proj/node_modules/babel-cli/lib/babel-node.js: line 1: /Applications: is a directory
/Users/me/proj/node_modules/babel-cli/lib/babel-node.js: line 3: /Applications: is a directory
/Users/me/proj/node_modules/babel-cli/lib/babel-node.js: line 4: Dockerfile: command not found
/Users/me/proj/node_modules/babel-cli/lib/babel-node.js: line 5: syntax error near unexpected token `('
/Users/me/proj/node_modules/babel-cli/lib/babel-node.js: line 5: ` * when found, before invoking the "real" _babel-node(1) executable.'

我推测这与从该目录调用可执行文件有关,而不是从与 server.js 文件相同的目录调用 - 但我真的不知道。

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/server.js",
            "stopOnEntry": false,
            "args": [],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": "${workspaceRoot}/node_modules/babel-cli/lib/babel-node.js",
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": false,
            "outDir": null
        },
        {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outDir": null,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }
    ]
}

【问题讨论】:

    标签: node.js visual-studio-code babel-node


    【解决方案1】:

    发生错误是因为babel-node.js 文件不是babel-node 可执行文件,而是一个添加节点标志的包装文件:

    babel-node.js

    /* eslint indent: 0 */
    
    /**
     * This tiny wrapper file checks for known node flags and appends them
     * when found, before invoking the "real" _babel-node(1) executable.
     */
    

    要解决此问题,应将babel-node 二进制文件的位置设置为runtimeExecutable 属性的值。地点是:

    "${workspaceRoot}/node_modules/.bin/babel-node"
    

    【讨论】:

    • 值得注意的是,我需要在路径中添加“.cmd”才能使其在 Windows 中运行
    • @cgatian 你能解释一下你是如何以及在哪里添加这个的吗?
    • 我相信的launch.json。
    • @cgatian 我的意思是你上面提到的.cmd,你在哪里添加的。我也在使用windows,无法正常工作
    • 这也适用于nvm 设置吗?我试了一下,尝试调试时出现版本错误。当前使用nvmnode 4.3.2node 6-something
    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2021-05-26
    • 2018-09-21
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多