运行 使用 Typescript 编译的 Nodemon?

Run Nodemon with Typescript compiling?

我希望在使用命令 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'",

这看起来会达到您的要求:

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

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

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

所以下面应该没问题:

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

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

tsc --watch

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

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

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

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

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

outDir可以定义在你的tsconfig

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

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

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

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

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