Run Nodemon with Typescript compiling?

可紊 提交于 2021-02-20 19:27:30

问题


I want my typescript files to be compiled on every file saving with the command tsc.

How do I combine the tsc command with the command that nodemon runs in the build:live script

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

this script causes nodemon to call itself twice or three times:

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

回答1:


This looks like it will achieve what you're looking for:

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

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




回答2:


Nodemon will detect and run .ts files with ts-node automatically now. It will actually run .py and .rb files with python and ruby too btw and you can give it a custom --exec for others. Here's a link to the relevant code within nodemon.

So the following should be fine:

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



回答3:


As of TypeScript 3.8+, you can now just use:

tsc --watch

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

You could then use nodemon on the compiled code, e.g. nodemon dist/app.js.




回答4:


With the current answer you might run into issues using ES modules. No need for nodemon when you're using tsc-watch. It makes use of incremental compilation, making the restart of your application much faster.

I found the following to work best:

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

The outDir can be defined in your tsconfig



来源:https://stackoverflow.com/questions/57998762/run-nodemon-with-typescript-compiling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!