Search code examples
debuggingcreate-react-appvisual-studio-code

VSCode debugging with Create-React-App


I'd like to set my VS Code to debug my React-app created using 'create-react-app'.

I've tried this configuration:

{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "Launch node",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/src/index.js",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "sourceMaps": true,
            "outFiles": []
        },
        {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 9222,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }
    ]
}

but I get error the following error:

Debugger listening on port 11198
e:\form\src\index.js:1
(function (exports, require, module, __filename, __dirname) { import React from 'react';
                                                             ^^^^^^

SyntaxError: Unexpected reserved word

...

I guess I need to set the 'preLaunchTask' to babel or the 'outFiles' to some dist folder, but I have no idea where should I point it to.

I'll be grateful for ideas. tnx


Solution

  • Your config is not correct. It should be:

    {
      "version": "0.2.0",
      "configurations": [{
        "name": "Chrome",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:3000",
        "webRoot": "${workspaceRoot}/src",
        "userDataDir": "${workspaceRoot}/.vscode/chrome",
        "sourceMapPathOverrides": {
          "webpack:///src/*": "${webRoot}/*"
        }
      }]
    }
    

    For more details: https://create-react-app.dev/docs/setting-up-your-editor/#debugging-in-the-editor