Debug & Run Angular2 Typescript with Visual Studio Code?
I am trying to debug Angular2 typescript application with VS code https://angular.io/gu
This one is tried and tested -
Step 1: Install chrome debugger: simply open the Command Palette (Ctrl+Shift+P) inside VS Code and type Extensions: Install Extension command. When the extension list appears, type 'chrome' to filter the list and install the Debugger for Chrome extension. You'll then create a launch-configuration file.
[More Details of Step 1]
Step 2: Create and update launch.json file: Two example launch.json configs with "request": "launch". You must specify either file or url to launch Chrome against a local file or a url. If you use a url, set webRoot to the directory that files are served from. This can be either an absolute path or a path using ${workspaceRoot} (the folder open in Code). webRoot is used to resolve urls (like "http://localhost/app.js") to a file on disk (like "/Users/me/project/app.js"), so be careful that it's set correctly. update Content of launch.json file as follows-
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch localhost",
"type": "chrome",
"request": "launch",
"url": "http://localhost/some_name",
"webRoot": "${workspaceRoot}/wwwroot"
},
{
"name": "Launch index.html (disable sourcemaps)",
"type": "chrome",
"request": "launch",
"sourceMaps": false,
"file": "${workspaceRoot}/index.html"
},
]
}
[More Details of Step 2]
After lot of research, I found these steps-
Before you begin, make sure you have latest version of VS code. You can verify latest version – Help > Check For Updates or About.
Install extension called 'Debugger for Chrome'. Once install complete, restart VS code.
Go to Debug window, open launch.json using Chrome.
In Launch.json configuration section, use below config
{
"name": "Launch localhost with sourcemaps",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000/Welcome",
"sourceMaps": true,
"webRoot": "${workspaceRoot}"
}
In tsconfig.json, make sure you have "sourceMap": true
This completes your debug environment settings. Now, before you start debugging, make sure all your existing Chrome.exe instances are closed. Verify from Task Manager OR Use DOS command ‘killall chrome’
Run your project, using npm start command and Chrome as default browser.
Once application is run successfully, you will receive port number. Copy URL from chrome browser and paste into url section above. (NOTE: If you are using routing in your application then url would like above otherwise it will be ending index.html etc)
Now, place breakpoints wherever you want in your typescript files.
Again, go to debug window in VS code, and hit Run. Your tab/instance connected to debugger will looks like below.
I had issues with this and while @Sankets answer helped it was this issue that solved it for me https://github.com/angular/angular-cli/issues/2453.
Specifically adding the below to launch.json
"sourceMapPathOverrides": {
"webpack:///c:/foo/bar/angular-project/*": "${workspaceRoot}/*"
}
These mods to launch.json
worked for me on MacOS, which enabled me to get the debugger & breakpoints working in a new instance of Chrome per debug session...
// This forces chrome to run a brand new instance, allowing existing
// chrome windows to stay open.
"userDataDir": "${workspaceRoot}/.vscode/chrome",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"sourceMapPathOverrides": { "webpack:///./*": "${webRoot}/*" }