1

I want to debug a React JS app on Visual Studio Code so i'm following this tutorial: https://code.visualstudio.com/docs/nodejs/reactjs-tutorial

I'm stuck at:

Ensure that your development server is running ("npm start"). Then press F5 or the green arrow to launch the debugger and open a new browser instance.

A new Chrome instance is being opened requesting "http://localhost:3000" but my app is not really running, it seems it just ran the debugger.

If i mannually npm start my app it runs. So i guess that launch.json is missing something for Visual Studio Code to start the app with the debugger. That's why i'm stuck at ensuring that my development server is running the command, because i don't know where exactly i check this

This is my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceRoot}"
        },
        {
            "type": "chrome",
            "request": "attach",
            "name": "Attach to Chrome",
            "port": 9222,
            "webRoot": "${workspaceRoot}"
        }
    ]
}

And this is my package.json:

{
  "name": "tic-tac-toe",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-scripts": "1.0.11"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}
0

2 Answers 2

7

You can set preLaunchTask in your launch.json to automatically run npm start.

Create tasks.json in .vscode folder.

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "start",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "isBackground": true,
            "problemMatcher": {
                "owner": "custom",
                "pattern": {
                    "regexp": "ˆ$"
                },
                "background": {
                    "activeOnStart": true,
                    "beginsPattern": "Compiling...",
                    "endsPattern": "Compiled .*"
                }
            }
        }
    ]
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Chrome",
      "type": "chrome",
      "preLaunchTask": "npm: start",
      "request": "launch",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/src",
      "sourceMapPathOverrides": {
        "webpack:///src/*": "${webRoot}/*"
      }
    }
  ]
}

Press F5 to start debugger, npm start will be executed.

Sign up to request clarification or add additional context in comments.

2 Comments

Code-only answers are not encouraged, please add some context to explain how this answer will solve the problem in question.
Nice. Switching the selected answer to this one since this does exactly what I needed at that time
1

I'm not sure if that's even possible. The tutorial clearly says you should ensure that the development server already runs. So, you'll have to first run npm startand debug afterwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.