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

时间:2016-07-08 23:46:05

标签: typescript npm nodemon tsc ts-node

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

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

tsc --watch已执行,但永远不会调用nodemon,反之亦然。

8 个答案:

答案 0 :(得分:68)

我认为你想要的是这样的(我当前的设置):

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

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

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

答案 1 :(得分:22)

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

就像在package.json中添加类似的东西一样简单:

"scripts": {
  "start": "./node_modules/.bin/tsc-watch --onSuccess \"node .\""
}

答案 2 :(得分:17)

尝试将此添加到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",
}

答案 3 :(得分:3)

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

第一
安装nodemonnpm install nodemon --save-dev)和ts-nodenpm 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应该运行并观看您的打字稿服务器代码。

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

答案 4 :(得分:1)

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),并通过使用yellow.bold添加颜色concurrently -c

因此,我可以很容易地识别出每个过程的消息。

答案 5 :(得分:1)

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

例如

"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进程之前睡了5秒钟,那么这两个tsc进程都已经完成,然后继续观看。

您也可以使用nodemon的delay选项,但是我只需要它在我执行npm run dev时第一次延迟。之后,每个项目中每个文件的每个单独的重新编译都只能正确重启一次nodemon。

caveat ,如果您的服务器运行缓慢,则可能需要将睡眠延迟增加到大于5。

我也尝试了接受的答案,但是当nodemon和tsc watch进程继续运行时,我的解决方案对于后续的重新编译速度更快。

我的解决方案

1秒,而接受的时间为5秒。我无法获得在监视模式下实际运行tsc的可接受答案,因此这是速度较慢的原因,因为两个TypeScript项目在每次更改时都得到了完全重新编译。

答案 6 :(得分:0)

发生了什么

问题是所有文件上都有两个观察者。一个是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)仅使monmon监视TSC的目标文件夹

我无法进行设置,但是这样nodemon希望只能检测到一个更改。将来或tsc生成多个文件时,可能会导致问题。

答案 7 :(得分:-1)

Normal compilation is: if file name is main.ts

step 1: tsc main.ts

step 2: node main.js

Simple and Onetime(loop) compilation:

tsc main --watch

相关问题