My Electron main process is written with TypeScript and bundled Webpack 2.
Transpilation is done through ts-loader and babel-loader.
Development mode starts webpack --watch with the main process configuration.
Problem
I cannot debug the main process using VSCode debugger.
Adding a breakpoint in the entry point src/main/index.ts does not have any effect.
Configuration
.vscode/launch.js
{
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"runtimeArgs": [
"${workspaceRoot}",
"--remote-debugging-port=9222"
],
"sourceMaps": true
}
]
}
webpack.development.js
{
target: 'electron',
devtool: 'source-map',
entry: {
main: join(__dirname, 'src/main/index')
},
output: {
path: join(__dirname, 'app'),
filename: '[name].js'
}
}
VSCode configuration
The important thing is to give VSCode the source file which is the entry point of the program, and specify it in "program" key.
You also need to specify in "outFiles" the bundle generated by Webpack.
{
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
// This is the important stuff
"program": "${workspaceRoot}/src/main/index.ts"
"outFiles": [
"${workspaceRoot}/app/main.js"
],
"sourceMaps": true
}
]
}
Webpack configuration
In your Webpack config you need to specify that you want to write the full paths of the modules source files in your sourcemaps.
{
output: {
devtoolModuleFilenameTemplate: '[absolute-resource-path]'
}
}
Also be careful to choose a sourcemap that is not evaluated, to permit VSCode to find the corresponding entry-point statically.
Minimal example
I created a repo with the minimal configuration and more explanations.
I don't know whether it's possible or not, but --remote-debugging-port=9222 is for the v8-inspector protocol, which isn't supported by Electron Node yet (https://github.com/electron/electron/issues/6634).
Since this is a launch config, VS Code will pass --debug=5858 to the runtime, so you don't need to specify a port here. Maybe try adding --nolazy. Hope that helps.
Related
So I have this really neat project with some really neat debugging which has worked since forever but after a too long hiatus and now I can't seem to get it to work. Like I noticed this in the debug tab
Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/inaba/Programming/Lewd/packages/main-site/src/index.js:1
import express from "express";
Here's my launch.json, which has worked for like forever
{
"type": "node",
"request": "launch",
"name": "Launch Main App",
"preLaunchTask": "npm: build",
"program": "src/index.js",
"sourceMaps": true,
"outFiles": [
"dist/**/*.js"
],
"envFile": "${workspaceFolder}/../../.env",
"runtimeArgs": [
"--require=dotenv/config"
]
}
So what the heck? As far as I can tell I'm doing things right?
Try require() instead of the import statement
Citing Node documentation here:
import statements are permitted only in ES modules. For similar
functionality in CommonJS, see import().
https://nodejs.org/api/esm.html#esm_import_statements
Some detailed context here: Using Node.js require vs. ES6 import/export
If you want to use that --trace-warnings in vscode debug, just add it into runtimeArgs.
don't forget to stop the debugging session if it is running
{
"type": "node",
"request": "launch",
"name": "Launch Main App",
"preLaunchTask": "npm: build",
"program": "src/index.js",
"sourceMaps": true,
"outFiles": [
"dist/**/*.js"
],
"envFile": "${workspaceFolder}/../../.env",
"runtimeArgs": [
"--require=dotenv/config",
"--trace-warnings"
]
}
Hello I am following this tutorial
https://code.visualstudio.com/docs/typescript/typescript-tutorial
to debug typescript but I have encountered error as shown in the screenshot.
If I choose debug anyway, the debugger works but I cant set any breakpoint. I suspect it has something to do with failing to set up the task file.
Any advise guys?
I had a similar problem. In my case, the tsconfig.json file was not in the main folder, but in a subfolder. In my case, the working configuration looked like this
{
"type": "node",
"request": "launch",
"name": "Launch server",
"program": "${workspaceFolder}/server/index.ts",
"preLaunchTask": "tsc: build - server/tsconfig.json",
"outFiles": ["${workspaceFolder}/out/**/*.js"]
}
Task tsc: build - tsconfig.json by default comes from VSCode when it detects the existence of tsconfig.json. Based on your screenshot, I can tell that you already have the file. So, it is odd if it can't be detected.
Please make sure the file content of tsconfig.json is valid.
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "out",
"sourceMap": true
}
}
Also to check whether the tasks are exist, you can choose menu Terminal -> Run Build Task or press Shift + Command + B on MacOS. If correct, you can see two tasks available there as the image below.
Otherwise, there must be something wrong with the steps. Perhaps, there is an extra space in preLaunchTask. For reference, I also copy paste my launch.json here.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/helloworld.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}
For other language users, the tsc: build command maybe be another command, such as tsc: 构建 in Chinese.
my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.ts",
"preLaunchTask": "tsc: 构建 - tsconfig.json",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}
When you Ctrl+Shift+B terminal will run Run Build Task.. You'll see in the terminal
">Executing task: tsc -p c:....
Terminal will be reused by tasks, press any key to close it."
Then after that double click a ts file and press F5 you will have to select at which environment do you want your ts file to run and then you will see the output in the debug console.
because it does not find path of tsconfig file....
see your folder structure whether the structure contains multiple same folder with same name...so y debugger confused to find path....so make sure the devlopement folder which you work on has proper path with unique name no same name with its parent folder and contains tsconfig files...
This happens also if the Extension Host crashed. This prevents the task engine from finding the requested task. Usually, you should see a message in the toaster where you can immediately restart the Extension Host. After that the debugger and tasks work.
For Ubuntu Linux 20.04 LTS (but may well be the same on other OS's) what got preLaunchTask working for me, was using both a local tasks.json and launch.json
So my folder structure (pre-build) is:
.vscode/launch.json
.vscode/tasks.json
dist
src/index.ts
package.json
tsconfig.json
My launch.json contains:
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "TS preLaunchTask-build",
"program": "${file}",
"preLaunchTask": "tsc: build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"skipFiles": [
"<node_internals>/**", "node_modules",
]
},
]
}
My tasks.json contains:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"command": "echo hello yes working!",
"problemMatcher": [],
"label": "myTask"
},
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": ["$tsc"],
"group": "build",
"label": "tsc: build"
},
]
}
And my tsconfig.json contains:
{
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"target": "es5",
"module": "commonjs"
},
"include": [
"src/**/*"
]
}
Usage: whilst within the index.ts just hit F5 with a breakpoint set
I have also included another task, "myTask" you can change the preLaunchTask line in your launch.json to: "preLaunchTask": "myTask", (where it will output some text to console to verify preLaunchTask is now working)
That way, if you still have issues, you can see if the issue is in your tsconfig setup, or if it's a preTaskLaunch setup issue.
(I would have thought it should have resolved this itself, but apparently not at the current time of writing anyway - but does force the advantage (for me) of committing debug config to the repo on project basis rather than global config)
Be sure to check the exact structure/spelling of Task by pressing Ctrl+Shift+B (on Windows) and Cmd+Shift+B(on Mac).
In my case, it was tsc: build - projectName/tsconfig.json
I'm trying to get vscode to launch mocha and stop on breakpoints. When I run the tests manually I use the following command:
$ mocha -r node_modules/reflect-metadata/Reflect.js --recursive
I can also use the following command:
mocha -r node_modules/reflect-metadata/Reflect.js --recursive --debug-brk
And the following debug config:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": true,
"outFiles": [
"src/**/**.js",
"test/**/**.test.js"
]
}
]
}
This allows me to set breakpoints in the .js files and see the original TypeScript source. But I can't set a break point directly in the TypeScript code.
My second problem is that I would like to simply press debug in the VSCode UI and trigger mocha in debug mode automatically and again hit breakpoints directly in the .ts files.
Is this possible?
I had already a very similar setup as #JasonDent but that didn't work. The node2 setting is already outdated (vscode will warn you). Instead simply add "protocol": "inspector" and voilá breakpoints are hit now:
{
"name": "Mocha",
"type": "node",
"protocol": "inspector",
"request": "launch",
"cwd": "${workspaceRoot}",
"preLaunchTask": "tsc",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": [ "--no-timeouts", "--colors", "${workspaceRoot}/out/test/**/*.js" ],
"stopOnEntry": false,
"runtimeExecutable": null,
"env": {
"NODE_ENV": "testing"
},
"sourceMaps": true
},
Here's my config based on the latest task building in VSCode. Out the box it doesn't work with Typescript!? Anyway combining the answer from #Jason Dent I was able to get it working! Its also using the newer node2 debugger. For your setup, change the build/test to where ever you have put your files.
{
"type": "node2",
"request": "launch",
// Automatically stop program after launch.
"stopOnEntry": false,
"name": "Mocha Tests",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha.cmd"
},
"runtimeArgs": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"--recursive",
"${workspaceRoot}/build/test"
],
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/build"],
"internalConsoleOptions": "openOnSessionStart",
// Prevents debugger from stepping into this code :)
"skipFiles": [
"node_modules/**/*.js",
"<node_internals>/**/*.js"
]
},
I also recommend a separate launch profile for debugging Mocha tests. I have the following configs, working with Mocha Typescript test.
My launch.json debug mocha profile looks like the following:
{
"type": "node",
"request": "launch",
"name": "Debug tests",
"runtimeExecutable": "mocha",
"windows": {
"runtimeExecutable": "mocha.cmd"
},
"preLaunchTask": "build:tests",
"runtimeArgs": [
"--debug-brk",
"-p",
"tsconfig.test.json",
"test-js/test/index.js"
],
"program": "${workspaceRoot}\\test\\index.ts",
"outFiles": [
"${workspaceRoot}\\test-js\\**\\*.js"
],
"port": 5858
},
build:tests is a vs code task, that runs 'tsc -p tsconfig.test.json'. I had some issues in the past with gulp-typescript sourcemap generation, that's why I'm using TSC at the moment.
My tsconfig.test.json is:
{
"compilerOptions": {
"outDir": "./test-js",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2016",
"declaration": true,
"sourceMap": true,
"inlineSources": true
},
"files": [
"./test/index.ts"
]
}
I hope you figured it out by now.
Basic answer, yes you can set breakpoints in .ts files and debug them with VSCode. They have a walk through here on general debugging: Debugging with VSCode
The key part is that you need to make a launch profile explicitly for mocha. This is just an example of how I got it to work. You will need to add something like the following to your .vscode/launch.json.
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Run mocha",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Request type "launch" or "attach"
"request": "launch",
// Workspace relative or absolute path to the program.
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program (mocha in this case).
"args": ["--recursive", "lib/*.test.js"],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": "${workspaceRoot}",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
"outDir": "${workspaceRoot}/lib",
"sourceMaps": true,
// Environment variables passed to the program.
"env": { "NODE_ENV": "test"}
}
This will launch mocha to test *.test.js files in the lib directory.
I used the following tsconfig.json file and have my unit tests next to the code:
{
"compilerOptions": {
"target": "es5",
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./lib",
"sourceMap": true,
"removeComments": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"data",
"lib"
]
}
I have the following directories:
.vscode
-- launch.json
-- tasks.json
build
-- script
-- -- main.js
-- -- main.js.map
node_modules
src
-- script
-- -- main.ts
-- typings
-- tsd.json
gulpfile.js
And of course other things like readmes and the content of node_modules etc.
Anyway, my launch.json looks as following:
{
"version": "0.2.0",
"configurations": [
{
"name": "debug",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/src/script/main.ts",
"stopOnEntry": true,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"preLaunchTask": "build",
"outDir": "${workspaceRoot}/build/script/",
"sourceMaps": true
},
{
"name": "attach",
"type": "node",
"request": "attach",
"port": 5858
}
]}
and my gulpfile for building the typescript as follows:
gulp.task('typescript', function() {
gulp.src('./src/script/**/*.ts')
.pipe(ts({
noImplicitAny: true,
target: 'ES6',
module: 'commonjs',
removeComments: true
}))
.pipe(sourcemaps.init())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./build/script/'));
});
Now back to my problem:
As said in the title, VS Code starts the debugger and it actually works but it debugs the main.js file instead of the main.ts file.
As far as I understand I have to set main.ts as the program parameter and the directory containing the js files as the outDir parameter.
At the moment I am somewhat stuck as I feel like my configuration is correct but for some reason, well obviously, it isn't.
Maybe someone can see the mistake I have made :)
I had a similar problem. First, try to execute the js file from your launch.json.
Then, you also need a tsconfig.json file in the root of the project, even if you compile with gulp. This file has a "sourceMap" parameter that needs to be set to true in order to be able to debug ts files with source maps.
A sample tsconfig.json file:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
I downloaded the Visual Studio Code, but I don't know how to configure the debugger.
I am learning programming and i don't know how to configure it?
someone help me with this problem?
This is what you need to configure.
{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch app.js",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "app.js",
// Automatically stop program after launch.
"stopOnEntry": true,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { }
},
{
"name": "Attach",
"type": "node",
// TCP/IP address. Default is "localhost".
"address": "localhost",
// Port to attach to.
"port": 5858
}
]
}
You need to be more specific with your question.
What kind of code are you debugging?
Right now Visual Studio Code has debugging support for Node.js (JavaScript and TypeScript) on all platforms, experimental support for mono (C# and F#) on OS X and Linux, and soon ASP.NET 5.
If you are using one of those, you should be able to start using debugging features.
For more information, here is a direct link to the official debugging documentation: https://code.visualstudio.com/Docs/debugging
If you are coding with php you can follow these step
You can copy and paste code:
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]