简体   繁体   English

如何在 Visual Studio 代码中调试“create-react-app”?

[英]How to debug `create-react-app`s in visual studio code?

I tried suggestions made here and in other places, but can't get the vscode debugger to work properly, IE breakpoints never become active 🔴 and of course they don't break.我尝试了在这里和其他地方提出的建议,但无法让 vscode 调试器正常工作,IE 断点永远不会激活🔴当然它们也不会中断。

The application is normally ran with npm start which calls react-scripts start .该应用程序通常使用调用react-scripts startnpm start运行。

I've tried these launch configurations:我已经尝试过这些启动配置:

  {
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Launch Chrome against localhost",
        "type": "pwa-chrome",
        "request": "launch",
        "url": "http://localhost:3000",
        "webRoot": "${workspaceFolder}"
      },
      {
        // I adapted this from a config to debug tests
        "name": "create-react-app",
        "type": "node",
        "request": "launch",
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
        "args": ["start"],
        "cwd": "${workspaceRoot}",
        "protocol": "inspector",
        "console": "integratedTerminal"
      }
    ]
  }

Your first launch configuration is fine, you just need to start the development server using npm start from a separate terminal, then press F5 or the green arrow to launch the debugger and open a new browser instance.你的第一个启动配置很好,你只需要从一个单独的终端使用npm start启动开发服务器,然后按F5或绿色箭头启动调试器并打开一个新的浏览器实例。

Reference: Debugging React参考: 调试 React

.vscode/launch.json .vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-chrome",
      "request": "launch",
      "name": "Launch Chrome against localhost",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}"
    }
  ]
}

It is sample projecet react它是示例项目反应

npx create-react-app my_app

1 Create Launchjsonfile if you haven't. 1 如果没有,请创建 Launchjsonfile。 this automatically create这会自动创建

在此处输入图像描述

  1. select web app (chrome)选择网络应用程序(铬)

在此处输入图像描述

3.change port number 8080(default) to 3000 (or your assigned port) 3.将端口号8080(默认)更改为3000(或您分配的端口)

在此处输入图像描述

4.Take package.json. 4.取package.json。 you can see the scripts and you focus on start word the the debug script will automatically suggest click on it .the application run on port localhost:3000您可以看到脚本并专注于启动词调试脚本将自动建议单击它。应用程序在端口 localhost:3000 上运行

在此处输入图像描述

or或者

在此处输入图像描述

5.you can check on browser 5.你可以在浏览器上查看

在此处输入图像描述

6.now add break point on the starting of the application here (where you want) 6.现在在应用程序的开始处添加断点(你想要的地方)

在此处输入图像描述

  1. click Launch chrome单击启动 chrome

在此处输入图像描述

  1. refresh page .now you can see the break point hit刷新页面。现在可以看到断点命中了

在此处输入图像描述

How it works(Gif) click on it for better quality它是如何工作的(Gif)点击它以获得更好的质量

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM