有没有办法使用 npm 脚本 运行 tsc -watch && nodemon --watch?

Is there a way to use npm scripts to run tsc -watch && nodemon --watch?

我正在寻找一种方法来同时使用 npm 脚本 运行 tsc --watch && nodemon --watch。我可以独立 运行 这些命令,但是当我想要 运行 两个命令时,只执行第一个。 例如:

"scripts": {    
    "runDeb": "set NODE_ENV=development&& tsc --watch && nodemon --watch"
  }

tsc --watch 被执行但 nodemon 从未被调用,反之亦然。

尝试将此添加到您的 package.json:

"scripts": {
  "start": "concurrently --kill-others \"tsc -w\" \"nodemon dist/app.js\"",
}

同时将这个 npm 包(同时,nodemon,typescript)添加到你的 package.json:

"devDependencies": {
  "concurrently": "^2.2.0",
  "typescript": "^1.8.10",
  "nodemon": "^1.9.2",
}

我想你想要的是这样的(我目前的设置):

"scripts": {
    "compile": "tsc && node app.js",
    "dev": "./node_modules/nodemon/bin/nodemon.js -e ts  --exec \"npm run compile\""
}

我创建了两个脚本 "compile" 和 "dev"。要开始开发,您只需 运行 npm run dev 即可启动 nodemon 并使其监视 .ts 文件(使用 -e 标志)。然后,每次 .ts 文件更改时,nodemon 将 exec 编译任务,基本上编译和 运行s 节点应用程序。

虽然同时使用是一个不错的选择,但我的设置保证 tsc 的工作在尝试执行生成的 .js 文件之前完成。

我已经使用 AlterX 的解决方案一段时间了,它运行良好,但我发现它相当慢。相反,我现在使用 tsc-watch。它使 tsc 使用类似于 -w 标志的增量编译,使应用程序的重启更快。

这就像在您的 package.json:

中添加类似的内容一样简单
"scripts": {
  "start": "tsc-watch --onSuccess \"node .\""
}

我在 2018 年 10 月 的解决方案使用 最新版本的 nodemon

第一:
安装 nodemon(npm install nodemon --save-dev) 和 ts-node(npm install ts-node --save-dev)

秒:
创建一个 nodemon.json 。我喜欢将我的 nodemon 配置保存在一个单独的 nodemon.json 中,以使 npm 脚本更容易阅读。所以在项目根目录下创建nodemon.json,内容如下:

{
    "ignore": ["**/*.test.ts", "**/*.spec.ts", ".git", "node_modules"],
    "watch": ["src"], // your .ts src folder
    "exec": "npm start", // your npm script created in package.json
    "ext": "ts"
}

然后创建您的 npm start 脚本,例如:

"scripts": {
    ...
    "start": "ts-node src/server.ts",
    "dev:ts": "nodemon",
    ...
  }

然后 运行 npm run dev:tsyarn dev:ts 应该 运行 并观察 你的 typescript 服务器代码。

有关 Jest 单元测试等更多配置...您可以查看 this 文章

正常编译为: 如果文件名是 main.ts

第一步: tsc main.ts

第 2 步: 节点main.js

简单一次性(循环)编译:

tsc main --watch

TypeScript-Node-Starter

https://github.com/microsoft/TypeScript-Node-Starter/blob/master/package.json

"dev": "concurrently -k -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold\" \"npm run watch-ts\" \"nodemon ./dist/app.js\"",
"watch-ts": "tsc -w"

这里我们给 npm run watch-ts 命名 TypeScript(通过使用 concurrently -n)并使用 concurrently -c 添加颜色 yellow.bold

所以,我可以很容易地识别每个进程的消息。

怎么回事

问题是所有文件上都有两个观察者。一个是 tsc -w,一个是 nodemon

当对 .ts 文件进行更改时,tsc 会检测到并编译它,并在目标文件夹中创建 .js 版本。

现在从 Nodemon 的角度来看,它检测到两个变化(至少)——一个是 .ts,另一个是 .js。在第一次更改时它会自行重新启动,但在第二次更改时它不知道另一个“启动”已经在进行,因此它尝试再次重新启动但失败了。对我来说,这是一个 nodemon 错误——参见 https://github.com/remy/nodemon/issues/763.

解决方案

1) 使用tsc-watch --onSuccess

tsc-watch--onSuccess,你可以把 node 放在那里。这样你将只有 一个观察者。

2) 延迟 nodemon

您可以轻松地延迟 nodemon 重新启动(参见 --delay)。它需要最少的设置更改。

3) 让 nodemon 只监控 TSC 的目标文件夹

我无法设置它,但这样 nodemon 将有望只检测到一个变化。它可能会在将来或当 tsc 生成多个文件时引起问题。

这是另一种方法,在开始 nodemon 之前在 concurrently 命令中使用 sleep

例如,

"scripts": {
    "dev": "concurrently -k \"tsc -p ./src/server -w\" \"tsc -p ./src/client -w\" \"sleep 5 && nodemon ./dist/server/server.js\"",
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./dist/server/server.js"
  },

在我的情况下,我同时生成客户端和服务器打字稿项目,这导致nodemon在我执行npm run dev时实际启动了3次。但是如果我在启动nodemon进程之前sleep 5秒,那么两个tsc进程都已经结束了,然后继续看。

你也可以使用nodemon的延迟选项,但我只需要在第一次执行时延迟npm run dev。在那之后,无论哪个项目中的哪个文件的每个单独的重新编译,都只正确地重新启动 nodemon 一次。

caveat, If your server is slow, you may need to increase the sleep delay longer than 5.

此外,我确实尝试了接受的答案,但我的解决方案对于后续的重新编译更快,而 nodemon 和 tsc 监视进程继续 运行。

我的解决方案为 1 秒,而接受的解决方案为 5 秒。我无法在监视模式下得到实际 运行 tsc 的公认答案,所以这就是为什么它变慢的原因,因为两个 TypeScript 项目在每次更改时都得到了完全重新编译。

TL;DR;nodemon 观察 tsc 输出 的变化(即 .js 文件)

您希望 nodemon 设置为在 tsc --watch 完成时进行监视,正如某些人在其他评论中提到的那样,因此只需让它监视 tsc 的目标目录即可.js 个文件中的更改。

例如,在package.json中:

"scripts": {
  ...
  "watch": "tsc --build src/tsconfig.json --watch",
  "watch-tests": "nodemon --watch dist -e js --exec \"yarn run tests\"",
  "tests": "some script to run my tests",
  ...
}

src/tsconfig.json中:

{
...
  "compilerOptions": {
    "outDir": "../dist",
    ...
  },
...
}

在哪里

  • --watch <folder> 将指向您在 tsconfig.json 文件 compilerOptions->outDir 中定义的同一位置,
  • -e js 只会监视 javascript 文件中的变化,
  • --exec <some arbitrary thing to run> 让我们的 nodemon 运行 不仅仅是 node.js 脚本。

如果你想要 nodemon 运行 的东西是一个节点脚本,它可以进一步简化为 nodemon --watch dist -e js my-node-script.js

Note: If you find nodemon kicking off it's script too soon, you can increase the throttle delay for checking for changes with --delay

您可以直接运行 .ts 文件与ts-node。只需全局安装它,nodemon 将自动使用 ts-node