debugging in Visual Studio Code with babel-node
I was able to get it working following these steps:
Package.json
Ensure you have a build script with sourcemaps generation.
"scripts": {
"build": "babel src -d dist --source-maps"
}
tasks.json
Ensure you have the task that lets VS Code to build with the npm
script.
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "build",
"args": [ "run", "build" ],
"isBuildCommand": true
}
]
}
launch.json
Configure the script to build before launch with a preLaunchTask
, start the program
from the source entry point, but with the outDir
pointing to the dist folder and with sourceMaps
enabled.
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/src/server.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
"runtimeArgs": [ "--nolazy" ],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": true,
"outDir": "${workspaceRoot}/dist"
}
Now, each time you press F5, the babel
transpilation runs before the Node process starts, but with all sourcemaps synced. With it I was able use breakpoints and all other debugger things.
Here is what worked for me (None of the other solutions worked for me with vscode v1.33):
./project.json
"scripts": {
"build": "babel src -d dist --source-maps",
},
.vscode/task.json
{
"version": "2.0.0",
"tasks": [{
"label": "build-babel",
"type": "npm",
"script": "build",
"group": "build"
}]
}
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"preLaunchTask": "build-babel",
"name": "Debug",
"program": "${workspaceRoot}/src/server.js",
"outFiles": ["${workspaceRoot}/dist/**/*.js"]
}]
}
As of version 1.9, VS Code automatically tries to use source maps by default, but you have to specify outFiles
if the transpiled files are not in the same folder as the source files.
As an example, here are the relevant files. In this case, babel is transpiling from the src
folder to the lib
folder.
Note: The entries in package.json
and .vscode/tasks.json
are only required if you want VS Code to transpile the files before debugging.
.vscode/launch.json
Ctrl+Shift+P, >Debug: Open launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/lib/index.js",
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"outFiles": [
"${workspaceRoot}/lib/**.js"
]
}
]
}
Note: Only specify preLaunchTask
if you also set up the build
tasks in package.json
and .vscode/tasks.json
.
package.json
Ctrl+P, package.json
{
"scripts": {
"build": "babel src -d lib -s"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-preset-env": "^1.1.10"
}
}
Note: You may use a different version of babel-cli
and different babel presets.
.vscode/tasks.json
Ctrl+Shift+P, >Tasks: Configure Task Runner
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "build",
"args": ["run", "build"],
"isBuildCommand": true
}
]
}
Official VS Code Documentation
Source Maps
The Node.js debugger of VS Code supports JavaScript Source Maps which help debugging of transpiled languages, e.g. TypeScript or minified/uglified JavaScript. With source maps, it is possible to single step through or set breakpoints in the original source. If no source map exists for the original source or if the source map is broken and cannot successfully map between the source and the generated JavaScript, then breakpoints show up as unverified (gray hollow circles).
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.
The source map feature is controlled by the sourceMaps
attribute which defaults to true
starting with VS Code 1.9.0. This means that node debugging always tries to use source maps (if it can find any) and as a consequence you can even specify a source file (e.g. app.ts) with the program
attribute.
If you need to disable source maps for some reason, you can set the sourceMaps
attribute to false
.
If the generated (transpiled) JavaScript files do not live next to their source but in a separate directory, you must help the VS Code debugger locating them by setting the outFiles
attribute. This attribute takes multiple glob patterns for including and excluding files from the set of generated JavaScript files. Whenever you set a breakpoint in the original source, VS Code tries to find the generated JavaScript code in the files specified by outFiles
.
Since source maps are not automatically created, you must configure the transpiler you are using to create them. For TypeScript this can be done in the follwoing way:
tsc --sourceMap --outDir bin app.ts
This is the corresponding launch configuration for a TypeScript program:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch TypeScript",
"type": "node",
"request": "launch",
"program": "app.ts",
"outFiles": [ "bin/**/*.js" ]
}
]
}
Source
No need to transpile with @babel/node
Basic setup (sourcemaps - always)
Note the sourceMaps
and retainLines
options in .babelrc
:
{
"presets": [
"@babel/preset-env"
],
"sourceMaps": "inline",
"retainLines": true
}
And then in launch.json
:
{
"type": "node",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/index.js",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
"runtimeArgs": [
"--nolazy"
]
}
Advance setup (sourcemaps - development only)
You can tweak the above so only to generate source-maps/retainLines in development mode:
{
"presets": [
"@babel/preset-env"
],
"env": {
"development": {
"sourceMaps": "inline",
"retainLines": true
}
}
}
And:
{
"type": "node",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/index.js",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
"runtimeArgs": [
"--nolazy"
],
"env": {
"BABEL_ENV": "development"
}
}
Notes
- Currently
"type": "pwa-node"
(see more) doesn't work with this setup. - For
"--nolazy"
see this. "BABEL_ENV": "development"
- Unless a different value is set the default isdevelopment
, so adding this in the launch config is not essential (but does make things more explicit).