无法使用可视代码调试 Next.js 应用程序

viv*_*kar 7 debugging reactjs visual-studio-code next.js vscode-debugger

我正在学习 Next.js,并且想在可视化代码和 chrome 中进行调试。我尝试了 launch.json 的不同组合来在可视代码中调试 next.js 应用程序。我从堆栈溢出本身获取了代码之一。但这又是一次失败。您能帮助我如何使用 google chrome 在 Visual Studio 代码中的 Next.js 应用程序中进行调试吗?

下面是我的 launch.json 文件代码:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Example",
      "runtimeExecutable": "node",
      "runtimeArgs": ["--inspect", "node_modules/.bin/next", "dev"],
      "port": 9229,
      "cwd": "${workspaceFolder}/frontend",
      "sourceMapPathOverrides": {
        "webpack:///./*": "${workspaceRoot}/frontend/*"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的 .next.config.js 的代码

module.exports = {
  webpack(config) {
    config.devtool = 'cheap-module-eval-source-map'
    return config
  },
}
Run Code Online (Sandbox Code Playgroud)

我的前端 package.json

{
  "name": "frontend",
  "version": "1.0.0",
  "description": "Social networking app",
  "proxy": "http://127.0.0.1:8080",
  "main": "index.js",
  "scripts": {
    "dev": "cross-env NODE_OPTIONS='--inspect' next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "author": "Vivek padelkar",
  "license": "ISC",
  "dependencies": {
    "@ant-design/icons": "^4.7.0",
    "antd": "^4.16.13",
    "axios": "^0.24.0",
    "bootstrap": "^5.1.3",
    "next": "^12.0.3",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-toastify": "^8.1.0"
  },
  "devDependencies": {
    "cross-env": "^7.0.3"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的文件夹结构如下:

在此输入图像描述

外部 pacakge.json 代码(即路径:社交网络/pacakge.json”

{
  "name": "socialnetwoek",
  "version": "1.0.0",
  "description": "social network backend",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "start": "node backend/server",
    "server": "nodemon backend/server",
    "client": "npm run dev --prefix frontend",
    "all": "concurrently \"npm run server\" \"npm run client\""
  },
  "author": "Vivek Padelkar",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "colors": "^1.4.0",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "esm": "^3.2.25",
    "express": "^4.17.1",
    "express-async-handler": "^1.2.0",
    "joi": "^17.4.2",
    "mongoose": "^6.0.12",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "concurrently": "^6.4.0",
    "nodemon": "^2.0.15"
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在执行的步骤:

  1. 在根文件夹(即社交网络)中,我正在执行命令 npm run all
  2. 然后我按 F5 运行调试器。

下面是我的 vs 屏幕,我正在使用 chrome 运行我的 Next.js 应用程序,所有调试点都呈灰色。(用红色框突出显示)

在此输入图像描述

但没有任何作用。

viv*_*kar 1

终于找到了解决方案,我按照以下方式编辑了我的 launch.json ,一切都按预期工作,谢谢你们宝贵的时间。

launch.json
{
  "configurations": [
    {
      "name": "Launch Chrome",
      "request": "launch",
      "type": "pwa-chrome",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/frontend"
    },
    {
      "name": "Attach to Edge",
      "port": 9222,
      "request": "attach",
      "type": "pwa-msedge",
      "webRoot": "${workspaceFolder}/frontend"
    },
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome against localhost",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "Launch Edge",
      "request": "launch",
      "type": "pwa-msedge",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/frontend"
    },
    {
      "name": "Launch Microsoft Edge and open the Edge DevTools",
      "request": "launch",
      "type": "vscode-edge-devtools.debug",
      "url": "http://localhost:3000" // Provide your project's url to finish configuring
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)