【问题标题】:Loopback 4 Debugger nodemon SolutionLoopback 4 调试器 nodemon 解决方案
【发布时间】:2019-05-13 09:58:08
【问题描述】:

如何在 Visual Studio 代码中使用 nodemon 调试 loopback 4 / node 应用程序?

寻找在环回打字稿代码更改时重建应用程序的解决方案。带有调试选项。

问候,

凯尔维恩

【问题讨论】:

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


    【解决方案1】:

    我终于找到了调试 Loopback 4/node.js 的解决方案。如果有人有建议,请这样做,这是第一个真正做到我想要的解决方案。

    通过运行启动调试器:

    npm run debug
    

    使用 nodemon 运行以下命令

    nodemon --exec run debug
    

    通过单击 Visual Studio 代码中行号的左侧添加断点。

    然后在 Visual Studio Code 中以调试模式启动应用程序

    Visual Studio Code (top-bar) -> Debug -> Start Debugging
    

    package.json

    "debug": "npm run build && node --nolazy --inspect-brk=9229 .",
    "build": "lb-tsc es2017 --outDir dist"
    

    launch.json

    {
     "version": "0.2.0",
     "configurations": [
    {
      "type": "node",
      "request": "attach",
      "timeout": 1000000,
      "name": "Attach",
      "port": 9229,
      "restart": true
    }
       ]
    }
    

    tsconfig.json

    注意:这个文件默认是通过loopback扩展的,所以你不必修改它。

     {
      "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "ES5",
        "lib": ["es2015"],
        "allowJs": true,
        "skipLibCheck": true
      },
      "include": ["src"],
      "exclude": ["node_modules", "platforms"]
    }
    

    应用结构

    【讨论】:

    • 这在文档中是否可用,然后请提供链接。
    • 按照建议进行更改,为我工作。相应地更新了答案。
    • 给出错误:'run' is not recognized as an internal or external command, operable program or batch file. [nodemon] app crashed - waiting for file changes before starting...
    • 对我来说,--nolazy 标志有所不同,因为 async/await 函数
    【解决方案2】:

    在根项目源中创建自己的 nodemon.json 并将其放入文件中

    {
      "ignore": [
        "**/*.test.ts",
        "**/*.spec.ts",
        ".git",
        "node_modules"
      ],
      "watch": [
        "src"
      ],
      "exec": "npm start",
      "ext": "ts"
    }
    

    然后运行nodemon 就是这样。只需确保您在全局范围内安装了 nodemon。

    【讨论】:

    • 最简单的解决方案,恕我直言。
    【解决方案3】:

    这是nodemon替代解决方案。

    tsc-watch 是一个类似的工具,可以与loopback 4 一起使用。基本上,它的工作原理类似于 nodemon。要将tsc-watch 添加为开发包,

    1. 在您的项目位置运行 npm install tsc-watch --save-dev
    2. 将以下行添加到package.json > scripts

      "start": "node -r source-map-support/register .", "dev": "tsc-watch -b --onSuccess \"npm start\""

    3. 现在运行npm run dev

    欲了解更多详情,请访问 tsc-watch npmgithub

    【讨论】:

      【解决方案4】:

      感谢您在这里提供的所有答案,我已经设法找到适合我的设置的东西。我使用带有环回 4 的纱线。感谢@dat-ho 的起点。

      首先在全局范围内安装nodemon

      然后将 nodemon 配置添加到 package.json。我在devDependencies之后的文件底部添加了这个。

       "nodemonConfig": {
          "verbose": true,
          "watch": [
            "src/"
          ],
          "ignore": [
            "dist/*"
          ],
          "ext": "ts",
          "exec": "yarn run clean && yarn start"
        }
      

      与先前答案的变化是 nodemon 检测到 npm start 脚本并重新运行 npm start 正常,但它不会重建并且更改未显示在运行代码中。所以我添加了"exec": "yarn run clean && yarn start" 行来清理并重新运行npm start 命令。

      然后您可以将以下 start:dev 命令添加到 package.json 的 scripts 部分:

      "start": "node -r source-map-support/register .",
      "start:dev": "nodemon '--inspect'",
      

      从那里运行yarn start:dev 并在任何打字稿文件更改时重建。希望这对你们有用。我花了大量的研究来完成这项工作。希望环回家伙将来可以有类似的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-05
        • 1970-01-01
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多