无法让TypeScript监视我的项目并节点重新加载它

时间:2018-06-20 13:38:47

标签: node.js typescript nodemon

命令tsc -w -p server观看server目录,将TypeScript编译到dist/server文件夹中(dist/server/app.js是主要的Node脚本)。

命令nodemon -w dist/server dist/server/app.js监视dist/server文件夹,并在发生更改时重新加载dist/server/app.js

问题:如果我同时运行两个命令tsc会花费一些时间,但是如果nodemon不存在,dist/server/app.js就会启动得太早然而。

concurrently \"tsc -w -p server\" \"nodemon -w dist/server dist/server/app.js\"

另一方面,如果我依次运行命令,则会丢失nodemon输出(即服务器输出),因为tsc将监视更改并“窃取”控制台输出:

tsc -w -p server\ && nodemon -w dist/server dist/server/app.js

我已经用nodemonnpm-run-all(一种流行的替代方法)测试了这两种策略。

相关问题(可接受的答案不能解决问题):

1 个答案:

答案 0 :(得分:0)

nodemon添加1000ms的延迟为我解决了该问题。

https://github.com/remy/nodemon#delaying-restarting

nodemon.json

{
  "watch": ["build"],
  "ext": "js",
  "exec": "npm start",
  "delay": 1000
}

package.json

{
  "name": "demo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node build/index.js",
    "ts": "tsc -w",
    "nodemon": "nodemon",
    "code": "concurrently -n ts,nodemon npm:ts npm:nodemon"
  },
  "devDependencies": {
    "concurrently": "^4.1.0",
    "nodemon": "^1.18.9",
    "typescript": "^3.2.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "build",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}

npm run code