【问题标题】:Not able to attach debugger to already running nodejs web app on port 3000无法将调试器附加到端口 3000 上已经运行的 nodejs Web 应用程序
【发布时间】:2017-10-11 04:54:04
【问题描述】:

我无法将调试器附加到已经运行的 NodeJS Web 应用程序。

以下是我的 VSCode 启动配置:

 "configurations": [
    {
        "type": "node",
        "request": "attach",
        "name": "Attach",        
        "port": 3001
    },

我收到以下错误消息:

无法连接到运行时进程(原因:此套接字已被对方终止)。

现在,当我在上述配置中添加protocol : inspector 时,它会显示以下错误消息:

确保使用 --inspect 启动节点。无法连接到运行时进程,10000 毫秒后超时

我目前使用的是v8.6.0版本的NodeJS

以下是我在 package.json >> 脚本中的配置:

 "scripts": {
    "start": "npm run build && npm run watch --inspect",
    "build": "npm run build-sass && npm run build-ts && npm run tslint && npm run copy-static-assets",
    "serve": "nodemon dist/server.js",
    "watch": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve\"",
    "test": "jest --forceExit",
    "build-ts": "tsc",
    "watch-ts": "tsc -w",
    "build-sass": "node-sass src/public/css/main.scss dist/public/css/main.css",
    "watch-sass": "node-sass -w src/public/css/main.scss dist/public/css/main.css",
    "tslint": "tslint -c tslint.json -p tsconfig.json",
    "copy-static-assets": "node copyStaticAssets.js",
    "debug": "npm run build && npm run watch-debug",
    "serve-debug": "nodemon --inspect dist/server.js",
    "watch-debug": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\""
  },

文件夹结构:

【问题讨论】:

  • 你是如何运行节点服务器的?
  • 只需在 VS 终端中使用“npm start”命令
  • Ensure Node was launched with --inspect 我认为您需要使用--inspect 标志运行它。
  • 能否分享完整的命令。此外,当尝试运行附加配置时,它会多次给出以下错误消息:[Node] GET /json 404 2.859 ms - 143
  • npm start --inspect?或者,node bin/www --inspect

标签: node.js debugging visual-studio-code


【解决方案1】:

成功了!

您必须在指定脚本之前将其传递给服务。此外,您需要在脚本中明确指定端口和 URL:

"serve": "nodemon dist/server.js",

改成:

"serve": "nodemon --inspect=0.0.0.0:9229 dist/server.js",

当 URL 是 0.0.0.0 而不是 127.0.0.1 时,显然 vscode 有效。

然后,在launch.json中:

"configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Docker: Attach to Node",
      "port": 9229,
      "restart": true,
      "localRoot": "${workspaceFolder}/src",
      "remoteRoot": "./dist}
]

注意localRoot 和remoteRoot,这是我根据你给我看的图片放置的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2018-07-19
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多