Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch and reload ts-node when TypeScript files change

I'm trying to run a dev server with TypeScript and an Angular application without transpiling ts files every time.

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.

Thank you in advance!!

like image 718
Ieltxu Algañarás Avatar asked Jun 22 '16 22:06

Ieltxu Algañarás


People also ask

How do I run a TypeScript file with ts-node?

We can use the ts-node package to execute TypeScript files from the command line. Install it with npm or other package manager. After that, simply execute the TypeScript files with the command: ts-node filename.

Can Nodemon run ts file?

19.0, nodemon has inbuilt support for TypeScript files with help from ts-node that requires no manual configuration. By default, nodemon uses the node CLI as an execution program for running JavaScript files; for TypeScript files, nodemon uses ts-node as the execution program instead.

How do I watch TypeScript?

Once correctly configured, you can simply run the tsc command and have it compile all the TypeScript code in your project. If you want to have it watch the files for changes then you can simply add --watch to the tsc command. In the example above, I include all . ts files in my project (recursively).

Does ts-node use TSC?

json (recommended)​ ts-node automatically finds and loads tsconfig. json . Most ts-node options can be specified in a "ts-node" object using their programmatic, camelCase names.


2 Answers

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:

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.

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" 

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" } 

and then run nodemon with no arguments.

By virtue of doing this, you'll be able to live-reload a ts-node process without having to worry about the underlying implementation.

Cheers!


And with even older versions of nodemon:

nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts 

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" } 
like image 55
HeberLZ Avatar answered Sep 27 '22 20:09

HeberLZ


I've dumped nodemon and ts-node in favor of a much better alternative, ts-node-dev https://github.com/whitecolor/ts-node-dev

Just run ts-node-dev src/index.ts

like image 37
Mikael Couzic Avatar answered Sep 27 '22 22:09

Mikael Couzic