node.jsvisual-studio-codebabel-node

Running babel-node in visual studio code


Normally to start up via command line, I can type:

babel-node server.js

When I try to set this up so that breakpoints and what not work in visual studio code I receive:

/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.'

I surmise it has to do with the fact that the executable is being called from that directory, rather than from the same directory as the server.js file - but I really don't know.

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
        }
    ]
}

Solution

  • The error occurs because the babel-node.js file is not the babel-node executable but a wrapper file that adds node flags:

    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.
     */
    

    To fix this, the location of the babel-node binary should be set as the value of the runtimeExecutable property. The location is:

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