Skip to content

Debugging with babel-register + NodeJs does not work #5728

Closed

Description

@mattflix
  • VSCode Version: 1.0.0
  • OS Version: Mac OS 10.11.3

Setup Steps:

  1. Download and expand NodeJsWithBabelDebuggingExample.ZIP
    into a local folder.
  2. Open the Terminal and point it to the folder from Step 1.
  3. Run npm install (to install the Babel dependencies).
  4. Run npm start (to demonstrate that the project is working Node program) which should produce a few lines of output and no errors.

Steps to Reproduce (on Mac OS):

  1. In VS Code, open the folder created in the steps above.
  2. Press F5 to start debugging the program.
  3. Observe the debugger break at line 1 of ./index.js, which is a comment. (This is the first sign of trouble.)
  4. Repeatedly press F10 (to step) and observe the debugger break on various other lines of ./index.js that have nothing to do with the code being executed.

Apparently, the inline source map generated by Babel is either being missed, ignored, or misinterpreted. It is also possible that the source map may be faulty, but seeing as a similar setup using other tools (such as Chrome Dev Tools) results in accurate debugging (and only when the source map is supplied) suggests that this is not the case.

Btw, VS Code gives zero feedback (as far as I can see) as to whether a source map was found and/or is being used, making it almost impossible to diagnose this type of trouble.

A few questions:

  1. Have a done something wrong in this setup? Is there something wrong or missing in my launch.json or package.json (where the Babel settings are)?
  2. Are there some diagnostics that I can enable in VS Code to help determine what is going wrong?
  3. Should I even expect to be able to successfully perform source-level debugging of dynamically-transpiled code with VS Code like this, or not? (Other tools can do it, so I will be disappointed if this is not a supported use-case for VS Code.)

The VS Code web pages (https://code.visualstudio.com/docs/editor/debugging) certainly make this sound like I should reasonably expect debugging to work:

Source maps can be generated with two kinds of inlining:

  • Inlined source maps: the generated JavaScript file contains the source map as a data URI at the end (instead of referencing the source map through a file URI).
  • Inlined source: the source map contains the original source (instead of referencing the source through a path).

VS Code supports both the inlined source maps and the inlined source.

As far as I know, setting { "sourceMaps": "inline" } in the Babel configuration (which this project does via package.json) adheres to the first case ("inlined source maps") which the above quote from the VS Code web pages seems to suggest is supported.

Thanks.

Activity

  1. bpasero commented on Apr 25, 2016

    @bpasero
    Contributor
  2. added
    debug
    on Apr 25, 2016
  3. added
    feature-request
    on Apr 25, 2016
  4. weinand commented on Apr 25, 2016

    @weinand
    Contributor

    Matt Arnold (@mattflix) VS Code does support inlined source maps but it does not yet support dynamically generated source maps at all. We are working on this feature already (and it was planned that the April release would already include some experimental support for dynamically generated source maps).

    The babel-register setup is especially tricky because it maps the original source *.js to *.js.source and the generated JavaScript to the original source. This is not a big problem in a debug-only tool like node-inspector which gets all its files through node.js. But in VS Code it is much more difficult because we have to map the files seen by node.js back to the workspace files that VS Code sees. And VS Code does not know what babel-register is actually doing.

  5. changed the title [-]Debugging with babel-register + NodeJs + inline sourceMaps does not work well[/-] [+]Debugging with babel-register + NodeJs does not work[/+] on Apr 25, 2016
  6. mattflix commented on Apr 25, 2016

    @mattflix
    Author

    Andre Weinand (@weinand) Thanks for the reply. Good to know that the feature is being worked on.

    Fwiw, Chrome DevTools with a workspace configured (where it can read/write from the local file system, turning into a more or less full editor/debugger) manages to work well with the source maps produced by dynamic Babel transpilation.. so it can't be that tricky. :) It creates the illusion that you are debugging and editing only the original source files. It even subtly shades the lines in the code editor that have been mapped by the source map (which is as example of providing feedback about whether or not source maps have been used).

  7. weinand commented on Apr 25, 2016

    @weinand
    Contributor

    Matt Arnold (@mattflix) VS Code's node debugging is limited by the v8 debugger protocol, it cannot (yet) use the Remote Debugging Protocol. On the other hand the VS Code debugger supports many more languages/runtimes than the Chrome DevTools. One consequence of this is that the VS Code debugger doesn't know anything about source maps. Providing source map feedback in the UI requires more abstractions and protocol enhancements which we will add over time.

    BTW, how do you debug node.js with the Chrome DevTools?

  8. mattflix commented on Apr 25, 2016

    @mattflix
    Author

    Andre Weinand (@weinand) Thanks for the reply. I don't debug Node.js per se in the regular Chome DevTools, but I can debug projects that take advantage of dynamic Babel transpilation via SystemJS there. The source maps are provided inline (by Babel) and Chrome consumes them. Used in conjunction with a DevTools workspace, to point to my local files, the debugging experience is very good. Plus, I find that it is one of the fastest (most responsive) Javascript debugging environments out there.

    In any case, the source map issues are a bit orthogonal to the "pure Node" debugging case but, to answer your question, I can simulate Node-style module resolution using SystemJS by "pre-resolving" all my module locations and writing them to a configuration for SystemJS (a bit like how JSPM works) such that all my requires and imports work as they would running under Node.exe. Of course, I can't use Node-only APIs this way (fs, http), but I am usually debugging "pure" Javascript code, rather than anything Node-specific.

  9. 14 remaining items

  10. katopz commented on Dec 9, 2016

    @katopz

    Greg Sieranski (@wonbyte) I use F5 to debug and it run with node not babel-node as I mention in my blog.

    for babel-node you'll need
    https://github.com/katopz/universal-boilerplate/blob/master/.vscode/launch.json#L13
    and run it with F5 no terminal and babel-register need here.
    And you can't just put everything there it'll mess up, try start from working one. e.g. here which super easy to follow.

    Cheers

  11. wonbyte commented on Dec 9, 2016

    @wonbyte

    Todsaporn Banjerdkit (@katopz) when I hit F5 I see, 'There are not entries to pick from'

  12. mrukas commented on Dec 20, 2016

    @mrukas

    Todsaporn Banjerdkit (@katopz) The solution you provided in your blog, seems to work the best for me. I really wanted to use the node2 debugger, but the old node debugger works better. node2 does work, but the problem is that it doesn't show me the values of variables if i hover them. It's also nice that you mention to enable the retainLines option, because it doesn't work if you leave the option disabled. What exactly does this option do? Maybe i don't quite get the concept of this option, i thought the original line numbers always get mapped into the source file?!

    Thanks!

  13. osenvosem commented on Feb 22, 2017

    @osenvosem

    I'm also looking for a good way to debug babel transpiled code with vscode.

  14. mengxingshike2012 commented on May 21, 2017

    @mengxingshike2012

    I tried but failed, can it work with async features?

  15. weinand commented on May 21, 2017

    @weinand
    Contributor

    mengxingshike2012 yes, debugging async features works. But the use of babel could be a problem because of its dynamically generated JavaScript and source maps. I suggest that you add "protocol": "inspector" to your launch config.

  16. su-rm-rf commented on Jul 4, 2017

    @su-rm-rf

    Matt Arnold (@mattflix)
    i got it.

    lanunch.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Launch",
          "type": "node",
          "program": "${workspaceRoot}/app.js",
          "runtimeExecutable": "/usr/local/bin/babel-node",
          "sourceMaps": true
        }
      ]
    }

    .babelrc

    {
      "presets": ["env"],
      "retainLines": true
    }
    
  17. ccjoe commented on Aug 1, 2017

    @ccjoe

    vscode can't hit the breakpoint, too,
    just debug on chrome-dev-tools, without source map
    Is this being worked on?

  18. linomarcos commented on Oct 10, 2017

    @linomarcos

    su-rm-rf (@su-rm-rf) I tried your suggestion on launch.json and .babelrc but still have the same problem. My problem happens only on an "export default" for a class that has a async/await method.

    When I use "export async function" debugging works fine!

    If the class method is not async it works too!

    Here some example from my code:

    users.js - (On this file works fine!)

    import UserBusiness from './UserBusiness'
    
    export async function authenticate (req, res) {
      const result = await UserBusiness.authenticate(req.body)
      return result
    }
    
    

    UserBusiness.js - (On this too!)

    import BaseBusiness from './BaseBusiness'
    
    const base = BaseBusiness('Users')
    
    export async function authenticate (parameters) {
      return base.authenticate(parameters)
    }
    

    BaseBusiness.js - (On this start mess up!)

    export default class BaseBusiness {
      constructor (modelName) {
        this.modelName = modelName
      }
      async authenticate (params) {
        return params
      }
    }
    

    On the last file i can put the breakpoint, the debugger stops on the line and I can use the console.log to print the params value, but cannot see the params value only passing mouse over!

    Can u help me?

  19. Izhaki commented on Oct 12, 2017

    @Izhaki

    Lino Marcos (@linomarcos) You have explicitly stated that you don't have the issue mentioned - in all 3 cases the debugger stops.

    Your actual issue is well known and is still open.

  20. Izhaki commented on Oct 12, 2017

    @Izhaki

    So. Anyone still can't get this to work?

    Or can we close this ticket?

  21. roblourens commented on Oct 13, 2017

    @roblourens
    Member

    I will close this - using "inspector", I believe it works as well as it can.

  22. FelikZ commented on Oct 28, 2017

    @FelikZ

    In case somebody find it through google.

    electron 1.7* + babel-register example of debugging configuration that works for me.

    VSCode debug config:

            {
                "name": "Launch",
                "type": "node",
                "request": "launch",
                "stopOnEntry": false,
                "args": [],
                "cwd": "${workspaceRoot}",
                "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
                "runtimeArgs": [
                    "-r",
                    "babel-register",
                    "./app"
                ],
                "sourceMaps": true,
                "outFiles": []
            },

    package.json

      "devDependencies": {
        "babel-preset-es2015": "6.16.0",
        "babel-preset-es2016": "6.16.0",
        "babel-preset-react": "6.16.0",
        "babel-register": "6.16.0",
        "electron": "1.7.9",
        "electron-builder": "19.42.2",
        "electron-rebuild": "1.6.0",
        "eslint": "3.17.0",
        "gulp-util": "3.0.8"
      }

    .babelrc

    {
        "presets": ["es2016", "es2015", "react"],
        "sourceMaps": "inline",
        "retainLines": true
    }
    
  23. locked and limited conversation to collaborators on Nov 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

debugfeature-requestjavascript

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions