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
        }
    ]
}
Prescott
  • 7,312
  • 5
  • 49
  • 70

1 Answers1

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"
gnerkus
  • 11,357
  • 6
  • 47
  • 71
  • 4
    Its worth noting that I needed to add '.cmd' to the path to get it working in Windows – cgatian Jun 29 '16 at 00:58
  • @cgatian can you please explain how and where did you add this? – Ja9ad335h Oct 05 '16 at 14:19
  • the launch.json I believe. – cgatian Oct 05 '16 at 15:31
  • @cgatian i mean that `.cmd` you mentioned above, where did you add that. i'm also using windows and cant get it work – Ja9ad335h Oct 05 '16 at 19:33
  • 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-something` – Brandon Nov 25 '16 at 22:47
  • Thanks @gnerkus 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) – Ilan Jun 07 '17 at 10:55
  • And to answer my own question - adding `"protocol": "inspector"` to the launch config fixes the slowness problem - see https://github.com/Microsoft/vscode/issues/24764 – Ilan Jun 07 '17 at 11:11
  • In the case babel-node is global (when it's not installed at the project level), the key/value would be: "runtimeExecutable": "babel-node" – lukace Apr 06 '18 at 20:28