TypeScript 文件更改时如何监视和重新加载 ts-node [英] How to watch and reload ts-node when TypeScript files change
问题描述
我正在尝试使用 TypeScript 和 Angular 应用程序运行开发服务器,而无需每次都转译 ts 文件.
I'm trying to run a dev server with TypeScript and an Angular application without transpiling ts files every time.
我发现我可以用ts-node
运行.ts
文件,但我也想看.ts
文件并重新加载我的应用程序/服务器.一个例子是命令 gulp watch
.
What I found is that I can run .ts
files with ts-node
but I want also to watch .ts
files and reload my app/server. An example of this is the command gulp watch
.
提前谢谢你!!
推荐答案
您现在可以简单地 npm install --save-dev ts-node nodemon
然后运行 nodemon
使用 .ts
文件,它会正常工作:
You can now simply npm install --save-dev ts-node nodemon
and then run nodemon
with a .ts
file and it will Just Work:
nodemon app.ts
以前的版本:
Previous versions:
我一直在为我的开发环境做同样的事情,直到我注意到 nodemon
的 API 允许我们更改其默认行为以执行自定义命令.
I was struggling with the same thing for my development environment until I noticed that nodemon
's API allows us to change its default behaviour in order to execute a custom command.
例如,对于 nodemon
的最新版本:
For example, for the most recent version of nodemon
:
nodemon --watch "src/**" --ext "ts,json" --ignore "src/**/*.spec.ts" --exec "ts-node src/index.ts"
或者创建一个 nodemon.json
文件,内容如下:
Or create a nodemon.json
file with the following content:
{
"watch": ["src"],
"ext": "ts,json",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts" // or "npx ts-node src/index.ts"
}
然后不带参数运行 nodemon
.
通过这样做,您将能够实时重新加载 ts-node
进程,而无需担心底层实现.
By virtue of doing this, you'll be able to live-reload a ts-node
process without having to worry about the underlying implementation.
干杯!
还有更老版本的nodemon
:
nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts
或者甚至更好:将 nodemon 的配置外部化为具有以下内容的 nodemon.json
文件,然后运行 nodemon
,正如 Sandokan 建议的那样:
Or even better: externalize nodemon's config to a nodemon.json
file with the following content, and then just run nodemon
, as Sandokan suggested:
{
"watch": ["src/**/*.ts"],
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./index.ts"
}
这篇关于TypeScript 文件更改时如何监视和重新加载 ts-node的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!