【问题标题】:How to debug a node js project on chrome which run by npm run serve如何在 chrome 上调试由 npm run serve 运行的 node js 项目
【发布时间】:2019-08-08 16:37:07
【问题描述】:

通常我们喜欢 node --inspect your_file.js,

例如:node --inspect server.js

但是我的项目现在使用命令"npm run serve"来启动服务器 那么如何在 chrome 上调试呢?

在 package.json 中的脚本定义如下

"scripts": {
  "lint": "eslint .",
  "serve": "firebase serve --only functions",
  "shell": "firebase functions:shell",
  "start": "npm run shell",
  "deploy_bkp": "firebase deploy --only functions",
  "logs": "firebase functions:log",
  "test": "mocha  --timeout 10000 --reporter spec"
},

【问题讨论】:

  • 不能在自己使用的代码编辑器中调试吗?
  • 我用的是vscode,可以在vscode中调试node吗?
  • 下面添加了答案,请查看

标签: javascript node.js debugging package.json


【解决方案1】:

您是否尝试过在 node_modules 文件夹中使用带有 firebase 路径的“node --inspect”。

类似下面的东西?

"scripts": {
  "lint": "eslint .",
  "serve": "node --inspect .\node_modules\firebase-tools\lib\bin\firebase.js serve --only functions",
  "shell": "firebase functions:shell",
  "start": "npm run shell",
  "deploy_bkp": "firebase deploy --only functions",
  "logs": "firebase functions:log",
  "test": "mocha  --timeout 10000 --reporter spec"
}

【讨论】:

    【解决方案2】:

    由于您使用的是 VS Code,因此您可以使用内置调试器来调试您的 NodeJS 项目。您只需要设置您的launch.json 文件。查看您的 package.json 脚本,这样的东西应该可以工作。

     {
      "version": "0.2.0",
      "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/functions/src/index.js",
        }
      ]
    }
    

    将文件名替换为您的主脚本文件而不是index.js,例如server.js

    在此之后,只需设置断点并开始调试!

    【讨论】:

    • 但这只是调试js文件的方式,在我的情况下不起作用,请检查我的脚本
    【解决方案3】:

    我想调试我的测试文件,我遇到了类似的情况。我所要做的就是

    node --inspect path/to/binary file_name.js_or_subcommand
    

    例如

    node --inspect node_modules/.bin/firebase serve
    

    这就足够了,您将能够使用 chrome 连接到调试器。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2020-10-27
      • 2019-12-06
      • 1970-01-01
      • 2022-12-21
      相关资源
      最近更新 更多