Running babel-node in visual studio code
Asked Answered
A

1

10

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
        }
    ]
}
Augustusaugy answered 13/3, 2016 at 20:49 Comment(0)
A
16

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"
Acceleration answered 13/3, 2016 at 21:16 Comment(8)
Its worth noting that I needed to add '.cmd' to the path to get it working in WindowsClaudieclaudina
@Claudieclaudina can you please explain how and where did you add this?Nanoid
the launch.json I believe.Claudieclaudina
@Claudieclaudina i mean that .cmd you mentioned above, where did you add that. i'm also using windows and cant get it workNanoid
Will this work for nvm setups as well? I gave it a shot and I got version errors when I tried to debug. currently using nvm with node 4.3.2 and node 6-somethingVassell
Thanks @Acceleration this works for me. However I find the debugging experience painfully slow - it takes +- 10 seconds for vscode to determine the call stack and various local variable states, so I have to wait 10 seconds on every single-step. Do you know how to speed it up? it's a lot quicker with normal node code (i.e. without running it through babel-node)Interfertile
And to answer my own question - adding "protocol": "inspector" to the launch config fixes the slowness problem - see github.com/Microsoft/vscode/issues/24764Interfertile
In the case babel-node is global (when it's not installed at the project level), the key/value would be: "runtimeExecutable": "babel-node"Stentor

© 2022 - 2024 — McMap. All rights reserved.