vscode debug ES6 application

2019-02-01 08:39发布

I have VSCode 0.5.0. I set the compilerOptions flag to "ES6" and the editor started recognizing my ES6 code as correct. I have babel installed. My Mocha tests use the babel compilers and my tests pass. My app runs from the command line with no problems when I launch it with babel-node . When I debug the app from within VSCode, it starts up without the ES6 support, and the app fails for ES6 syntax issues. Are there debug settings that I missed turning on?

8条回答
家丑人穷心不美
2楼-- · 2019-02-01 08:59

babel-node & vs code attach

  1. config a npm script in package.json:

    "scripts": {
        "debug": "babel-node --debug-brk demo.js --presets es2015,stage-2"
    }
    
  2. add vs code debug configuration:

    {
        "name": "Attach",
        "type": "node",
        "protocol": "legacy",
        "request": "attach",
        "port": 5858
    }
    
查看更多
太酷不给撩
3楼-- · 2019-02-01 09:00

babel + nodemon

In the VS Code Terminal, launch your server with the --inspect argument:

nodemon --inspect --watch src --exec node_modules/.bin/babel-node --presets react,es2015 src/server.js

Among the other lines, one will show the port on which the debugger is listening:

...
Debugger listening on port 9229
...

Create the following debug configuration:

{
    "type": "node",
    "request": "attach",
    "name": "Attach to Port",
    "address": "localhost",
    "port": 9229
}

Launch the debugger, and if everything went fine you will see in the output Terminal:

Debugger attached.

From now on, you can debug your application.

查看更多
Evening l夕情丶
4楼-- · 2019-02-01 09:12

There are two ways of doing it:

First Option using npm command prompt

In package.json file create build command that will execute babel

{
  "scripts": {
    "build": "babel src --out-dir dist --watch --source-maps"
  },
  "devDependencies": {
    "babel-cli": "^6.23.0",
    "babel-preset-es2015-node6": "^0.4.0",
    "eslint": "^3.16.0"
  }
}

In launch.json Enter following code:

{
  "configurations": [
    {
      "name": "Launch",
      "type": "node",
      "request": "launch",
      "program": "${workspaceRoot}/src/index.js",
      "stopOnEntry": false,
      "args": [],
      "cwd": "${workspaceRoot}",
      "runtimeArgs": [
        "--nolazy"
      ],
      "sourceMaps": true,
      "outFiles": [
        "${workspaceRoot}/dist/**/*.js"
      ]
    }
  ]
}

Open your cmd window, navigate to your package.json file and execute:

npm run build

Open your VS Code and run your code. It will run and it will stop at all your breakpoints. The reason it works because source maps are generated and VS knows how to map them to your code.

Second option using VS Code task:

In VS Code add following task (Ctrl + Shift + P) and type 'Tasks: Configure Task Runner':

Add following code to tasks.json file

{
  "version": "0.1.0",
  "command": "${workspaceRoot}/node_modules/.bin/babel",
  "isShellCommand": true,
  "tasks": [
    {
      "taskName": "watch",
      "args": [
        "src",
        "--out-dir",
        "dist",
        "--watch",
        "--source-maps"
      ],
      "suppressTaskName": true,
      "isBuildCommand": true
    }
  ]
}

Now execute task, but pressing Ctrl + Shift + B (build command) and now you can run and debug your code. VS Code doing the same as what npm is doing in step one.

You will also need to configure babel in .babelrc (located in the root of the project) file like this:

{
  "presets": [
    "es2015-node6"
  ]
}

and jsconfig.json (located in the root of the project)

{
  "compilerOptions": {
    "target": "ES6"
  },
  "include": [
    "src/**/*"
  ]
}
查看更多
戒情不戒烟
5楼-- · 2019-02-01 09:15

Here's how to get VSCode debugger to work with Babel 6+:

First install dependencies locally:

$ npm install babel-cli --save
$ npm install babel-preset-es2015 --save

Then run babel-node:

$ node_modules/babel-cli/bin/babel-node.js --inspect --presets es2015 -- server.js --inspect

By default, the debugger will listen on port 5858, so make sure the port matches in launch.json for Attach configuration:

{
  "name": "Attach",
  "type": "node",
  "request": "attach",
  "port": 5858
}

Now attach the debugger in VSCode:

  • make sure debug configuration is set to Attach and not Launch
  • run with F5

Nodemon

Although not required, if you also want to use nodemon to pickup code changes without restarting the server, you can do this:

Make sure nodemon is installed:

$ npm install nodemon --save-dev

Run the server

$ node_modules/.bin/nodemon node_modules/babel-cli/bin/babel-node.js --inspect --presets es2015 -- server.js --inspect

Finally, attach the debugger as shown above.

查看更多
对你真心纯属浪费
6楼-- · 2019-02-01 09:19

You can try babel-register (you'll also need other companion babel modules as req'd):

npm install --save-dev babel-register

with a launch.json configuration along these lines:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/src/index.js",
            "stopOnEntry": false,
            "args": [],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy",
                "--require",
                "babel-register"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "console": "internalConsole",
            "sourceMaps": true,
            "outFiles": [
            ]
        },
        {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outFiles": [],
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        },
        {
            "name": "Attach to Process",
            "type": "node",
            "request": "attach",
            "processId": "${command.PickProcess}",
            "port": 5858,
            "sourceMaps": false,
            "outFiles": []
        }
    ]
}

This is loosely based on vscode-debug-nodejs-es6 with the addition of the babel-register runtime argument.

查看更多
三岁会撩人
7楼-- · 2019-02-01 09:20

When transpiling with bael-node, you should add "--inspect-brk" in the script, so that the script may break when a breakpoint is hit.

Ex:

"start": "babel-node --inspect-brk app.js --presets es2015,stage-2"

Now when you run using npm run start, debugger will be launched and you can see following in your console:

Debugger listening on ws://127.0.0.1:9229/cf72a33c-ab38-4798-be5e-8b8df072f724 For help see https://nodejs.org/en/docs/inspector

That shows debugging process has started and we can attach to it on port# 9229.

Now, you need to add the following debugger config for vs-code to attach to this process: (in launch.json)

{ "version": "0.2.0", "configurations": [ { "name": "Attach to Process", "type": "node", "request": "attach", "port": 9229 } ] }

After saving, click the "start debugging" button, to attach to process initiated by node earlier. You may read more about this here

查看更多
登录 后发表回答