在visual studio代码中运行babel-node

Pre*_*ott 10 node.js visual-studio-code babel-node

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

babel-node server.js
Run Code Online (Sandbox Code Playgroud)

当我尝试将其设置为断点以及在我收到的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.'
Run Code Online (Sandbox Code Playgroud)

我猜测它与从该目录调用可执行文件的事实有关,而不是与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
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

gne*_*kus 16

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

巴贝尔-的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.
 */
Run Code Online (Sandbox Code Playgroud)

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

"${workspaceRoot}/node_modules/.bin/babel-node"
Run Code Online (Sandbox Code Playgroud)

  • "$ {} workspaceRoot /node_modules/.bin/babel-node.cmd" (5认同)
  • 值得注意的是,我需要在路径中添加".cmd"以使其在Windows中运行 (4认同)