Debug & Run Angular2 Typescript with Visual Studio Code?

后端 未结 10 1047
情歌与酒
情歌与酒 2020-11-27 10:08

Debug & Run Angular2 Typescript with Visual Studio Code?

I am trying to debug Angular2 typescript application with VS code https://angular.io/gu

相关标签:
10条回答
  • 2020-11-27 10:37

    Sanket's answer was correct but I had some problems with that.

    First, Launch.json is in the ".vscode" directory of the project, and second, port number should be the same as the default server port you use for launching your app. I use ng serve from cmd for launching my project and the default port number was 4200, so I changed the launch.json file as follows.

    {
        // 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": "chrome",
                "request": "launch",
                "name": "Launch Chrome against localhost",
                "url": "http://localhost:4200",
                "sourceMaps": true,
                "webRoot": "${workspaceFolder}"
            }
        ]
    }
    
    0 讨论(0)
  • 2020-11-27 10:39

    For angular-seed:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Debug with chrome",
          "type": "chrome",
          "request": "launch",
          "url": "http://localhost:5555",
          "sourceMaps": true,
          "webRoot": "${workspaceRoot}/src/client",
          "userDataDir": "${workspaceRoot}/out/chrome",
          "sourceMapPathOverrides": {
            "app/*": "${webRoot}/app/*"
          }
        }
      ]
    }
    
    0 讨论(0)
  • 2020-11-27 10:41

    I was having a similar issue but my project also included webpack that caused the above solutions to fail. After traversing the Internet I found a solution in a thread on github:

    https://github.com/AngularClass/angular2-webpack-starter/issues/144#issuecomment-218721972

    Anyway, this is what was done.

    Note:- Before you start you must check whether you have the latest version of visual studio code and also have installed the extension called 'Debugger for Chrome' within VS Code.

    Firstly, in './config/webpack.dev.js'

    • use => devtool: 'source-map'
    • instead of => devtool: 'cheap-module-source-map'

    Then install and use the write-file-webpack-plugin:

    • npm install --save write-file-webpack-plugin

    Add the plugin to './config/webpack.dev.js' by adding:

    • const WriteFilePlugin = require('write-file-webpack-plugin');

    under the Webpack Plugins at the top. Continue to add:

    • new WriteFilePlugin()

    to the list of plugins after new new DefinePlugin(), i.e

    plugins:[
        new DefinePlugin({....}),
        new WriteFilePlugin(),
        ....
    ]
    

    This ensures that the source maps are written to disk

    Finally, my launch.json is given below.

    {
        "version": "0.2.0",
        "configurations": [{
            "name": "Launch Chrome against localhost, with sourcemaps",
            "type": "chrome",
            "request": "launch",
            "url": "http://localhost:3000/",
            "runtimeArgs": [
               "--user-data-dir",
               "--remote-debugging-port=9222"
            ],
            "sourceMaps": true,
            "diagnosticLogging": true,
            "webRoot": "${workspaceRoot}",
            "userDataDir": "${workspaceRoot}/.vscode/chrome"
        },
        {
            "name": "Attach to Chrome, with sourcemaps",
            "type": "chrome",
            "request": "attach",
            "url": "http://localhost:3000/",
            "port": 9222,
            "sourceMaps": true,
            "diagnosticLogging": true,
            "webRoot": "${workspaceRoot}"
        }]
    }
    

    notice the absence of '/dist/' in the webroot. with this config, source-maps are in ./dist/, but they point to ./src/. vscode prepends the absolute root to the workspace, and correctly resolves the file.

    0 讨论(0)
  • 2020-11-27 10:42

    Here is an official visual studio code documentation on how to debug Angular in VSCode https://code.visualstudio.com/docs/nodejs/angular-tutorial#_configure-the-chrome-debugger

    To debug the client side Angular code, we'll need to install the Debugger for Chrome extension. Open the Extensions view (Ctrl+Shift+X) and type 'chrome` in the search box. You'll see several extensions which reference Chrome. Press the Install button for Debugger for Chrome. The button will change to Installing then, after completing the installation, it will change to Reload. Press Reload to restart VS Code and activate the extension.

    We need to initially configure the debugger. To do so, go to the Debug view (Ctrl+Shift+D) and click on gear button to create a launch.json debugger configuration file. Choose Chrome from the Select Environment dropdown. This will create a launch.json file in a new .vscode folder in your project which includes configuration to both launch the website or attach to a running instance. We need to make one change for our example: change the port from 8080 to 4200.

    0 讨论(0)
  • 2020-11-27 10:44

    Specify a userDataDir - this will avoid collisions with other Chrome instances you may already have running. I've noticed because of this, Chrome just stops listening on any port you specify. Below is what I use and it is great!

    {
        "name": "Launch",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:3000/#/about",
        "port": 9223,
        "sourceMaps": true,
        "diagnosticLogging": true,
        "webRoot": "${workspaceRoot}",
        "userDataDir": "${workspaceRoot}/out/chrome"
    }
    

    Thanks to @reecebradley - GitHub: Cannot connect to the target: connect ECONNREFUSED

    0 讨论(0)
  • 2020-11-27 10:44

    I need to use CORS so I open chrome in with disabled web security. Then I do debugging of Angular application by attaching debugger to chrome.

    CMD line to launch chrome with disabled web security:

    cd "C:\Program Files (x86)\Google\Chrome\Application\"
    chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security --remote-debugging-port=9222
    

    launch.json file for attaching debugger:

    {
        // 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": "chrome",
                "request": "attach",
                "name": "Attach to Chrome",
                "port": 9222,
                "webRoot": "${workspaceFolder}"
            },
        ]
    }
    
    0 讨论(0)
提交回复
热议问题