【问题标题】:Run Nodemon with Typescript compiling?使用 Typescript 编译运行 Nodemon?
【发布时间】:2020-01-19 18:21:34
【问题描述】:

我希望在使用命令tsc 保存的每个文件上编译我的打字稿文件。

如何将 tsc 命令与 nodemon 在build:live 脚本中运行的命令结合起来

"scripts": {
    "start": "npm run build:live",
    "build:live": "nodemon --watch '*.ts' --exec 'ts-node' app.ts",
 }

这个脚本导致 nodemon 调用自己两次或三次:

"build:live": "nodemon --watch '*.ts' --exec 'ts-node app.ts & tsc'",

【问题讨论】:

    标签: javascript node.js typescript visual-studio-code nodemon


    【解决方案1】:

    这看起来会实现你想要的:

    "start": "tsc-watch --project . --outDir ./dist --onSuccess \"nodemon ./dist/bin/www.js\""
    

    来源:https://github.com/Microsoft/TypeScript/issues/12996#issuecomment-349277673

    【讨论】:

    • 这解决了我的确切问题。我希望 TS 进行转译,确保转译成功,然后在 dist.xml 中运行特定文件。谢谢!
    【解决方案2】:

    Nodemon 现在将自动检测并运行带有ts-node.ts 文件。它实际上会使用 python 和 ruby​​ 运行 .py.rb 文件,顺便说一句,你可以给它一个自定义的 --exec 给其他人。这是 nodemon 中的link to the relevant code

    所以以下应该没问题:

    "scripts": {
      "dev": "nodemon app.ts"
    }
    

    【讨论】:

      【解决方案3】:

      从 TypeScript 3.8+ 开始,您现在可以使用:

      tsc --watch
      

      https://www.typescriptlang.org/docs/handbook/configuring-watch.html

      然后您可以在编译后的代码上使用nodemon,例如nodemon dist/app.js.

      【讨论】:

      • 这是正确答案!谢谢。
      【解决方案4】:

      根据当前答案,您可能会在使用 ES 模块时遇到问题。 使用tsc-watch 时不需要nodemon。它利用增量编译,使您的应用程序的重启速度更快。

      我发现以下方法效果最好:

      "start": "tsc-watch --onSuccess \"node ./dist/app.js\""
      

      outDir 可以在您的tsconfig 中定义

      【讨论】:

        【解决方案5】:

        您可以在项目根目录中创建一个 nodemon.json 并在其中添加以下代码:

        {
         "ext": "*.ts",
         "exec": "tsc && ts-node app.ts"
        }
        

        并更新您的脚本,如下所示:

        "scripts": {
           "start": "npm run build:live",
           "build:live": "nodemon",
        }
        

        发生的情况是,nodemon 将检查所有扩展名为“.ts”的文件并启动 tsc,然后启动 ts-node。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-10
          • 2012-10-24
          • 2021-12-15
          • 2021-07-16
          • 1970-01-01
          • 2020-02-23
          • 1970-01-01
          • 2021-04-09
          相关资源
          最近更新 更多