Question
How to Watch and Reload ts-node When TypeScript Files Change
Question
I want to run a development server for a TypeScript project, including an Angular-related workflow, without manually transpiling .ts files after every change.
I found that ts-node can execute TypeScript files directly, but I also want the application or server to automatically watch .ts files and reload when they change, similar to how gulp watch works.
How can I set up automatic watching and reloading for ts-node when TypeScript files are updated?
Short Answer
By the end of this page, you will understand how ts-node fits into a TypeScript development workflow, why it does not automatically restart your app on file changes by itself, and how to pair it with file-watching tools like nodemon. You will also see practical command examples, common mistakes, and a small mini-project for creating a TypeScript dev server with automatic reloads.
Concept
ts-node lets you run TypeScript files directly in Node.js without manually compiling them first with tsc.
That solves execution, but not watching.
These are two separate concerns:
- Running TypeScript directly: handled by
ts-node - Watching files and restarting when they change: handled by a watcher such as
nodemon,node --watchin some setups, or similar tools
This distinction matters because many beginners expect ts-node to behave like a full development server. In reality, ts-node is mainly an execution tool. If your server is running and you edit a .ts file, Node does not automatically restart unless another tool is monitoring those files.
In real programming, this pattern is very common:
- one tool runs the code
- another tool watches for changes
- sometimes a framework combines both for convenience
For a TypeScript backend or local development server, a common setup is:
ts-nodeto execute.tsfilesnodemonto watch files and restart the process
This gives you a fast feedback loop during development without needing to run a full manual compile step each time.
Mental Model
Think of your development setup like a kitchen:
ts-nodeis the cook who knows how to prepare TypeScript and serve it immediately.- A watcher like
nodemonis the bell that notices when ingredients have changed and tells the cook to start over.
If you only have the cook, nothing happens when new ingredients arrive. If you only have the bell, it can ring, but no one knows how to run TypeScript.
You usually need both working together.
Syntax and Examples
The most common beginner-friendly setup is to use nodemon with ts-node.
Install the tools
npm install --save-dev ts-node typescript nodemon
Basic command
npx nodemon --watch src --ext ts --exec ts-node src/index.ts
What this does
--watch srcwatches thesrcfolder--ext tsrestarts only when.tsfiles change--exec ts-node src/index.tsruns your app withts-node
Example project file
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.();
});
server.(, {
.();
});
Step by Step Execution
Consider this file:
let count = 0;
console.log('Starting app...');
console.log('Count is', count);
And this command:
npx nodemon --watch src --ext ts --exec ts-node src/index.ts
What happens step by step
nodemonstarts running.- It begins watching the
srcdirectory. nodemonexecutes:ts-node src/index.tsts-nodereadssrc/index.ts.- It compiles the TypeScript in memory.
- It runs the resulting JavaScript in Node.js.
- The console shows:
Starting app... Count is 0 - You edit
src/index.tsand save the file. nodemonnotices that a.tsfile changed.
Real World Use Cases
Automatic watching and restarting is useful in many real projects:
API development
When building an Express or Fastify API in TypeScript, you want the server to restart immediately after route or controller changes.
Background workers
If you have TypeScript scripts that process queues, cron jobs, or scheduled tasks, watching helps during development and debugging.
CLI tools
When building a command-line tool in TypeScript, auto-reload speeds up testing changes.
Full-stack local development
A frontend app may already have hot reload, but your TypeScript backend still needs a separate watch-and-restart workflow.
Prototyping
During early development, skipping manual tsc builds makes iteration faster and simpler.
Real Codebase Usage
In real codebases, developers usually do not run long raw commands manually. They wrap them in scripts and configuration files.
Common pattern: package.json scripts
{
"scripts": {
"dev": "nodemon",
"build": "tsc",
"start": "node dist/index.js"
}
}
Then nodemon.json contains the development behavior.
Common pattern: separate dev and production flows
Development:
- run with
ts-node - watch with
nodemon - restart automatically
Production:
- compile with
tsc - run generated JavaScript from
dist
This separation matters because ts-node is usually best for development convenience, while compiled output is more common in production.
Common Mistakes
1. Expecting ts-node alone to watch files
Broken expectation:
ts-node src/index.ts
This runs the file once. It does not restart when files change.
Fix
Use a watcher:
npx nodemon --watch src --ext ts --exec ts-node src/index.ts
2. Watching the wrong folder
Broken example:
npx nodemon --watch app --ext ts --exec ts-node src/index.ts
If your files are actually in src, changes may not trigger restarts.
Fix
Match your real source directory.
3. Forgetting file extensions
Broken example:
npx nodemon --watch src --exec ts-node src/index.ts
This may still work in some cases, but being explicit is safer.
Fix
Specify TypeScript files:
npx nodemon --watch src --ext ts --exec ts-node src/index.ts
Comparisons
| Tool or approach | What it does | Watches files? | Runs TypeScript directly? | Typical use |
|---|---|---|---|---|
ts-node | Executes TypeScript files | No | Yes | Development execution |
nodemon | Watches files and restarts a process | Yes | No | Development auto-restart |
ts-node + nodemon | Runs TS and restarts on changes | Yes | Yes | Common TypeScript dev setup |
tsc --watch | Recompiles TypeScript on changes | Yes |
Cheat Sheet
# Install dev tools
npm install --save-dev typescript ts-node nodemon
# Run TypeScript with auto-restart
npx nodemon --watch src --ext ts --exec ts-node src/index.ts
package.json script
{
"scripts": {
"dev": "nodemon --watch src --ext ts --exec ts-node src/index.ts"
}
}
nodemon.json
{
"watch": ["src"],
"ext": "ts,json",
"ignore": ["dist", "node_modules"],
"exec": "ts-node src/index.ts"
}
Key idea
FAQ
Does ts-node automatically reload when files change?
No. ts-node runs TypeScript files directly, but it does not watch files and restart by itself.
What tool should I use with ts-node for auto-reload?
A common choice is nodemon. It watches file changes and reruns ts-node when needed.
Can I use this with an Angular project?
Yes, but usually for the backend or custom Node scripts. Angular frontend reloading is normally handled by Angular's own dev server.
Is ts-node meant for production?
It can be used in some setups, but many projects compile TypeScript with tsc and run the generated JavaScript in production.
Why does my app keep restarting in a loop?
You may be watching generated files like dist or other files that change during startup. Ignore those folders in your watcher config.
Should I use tsc --watch instead of nodemon?
Use tsc --watch if you want automatic compilation. Use nodemon with ts-node if you want direct execution and restart during development.
Mini Project
Description
Create a small TypeScript HTTP server that automatically restarts whenever you change a .ts file. This project demonstrates the exact development workflow behind watching and reloading with ts-node and nodemon.
Goal
Set up a local TypeScript server that you can run with one command and have it restart automatically when source files change.
Requirements
- Create a
src/index.tsfile that starts a basic HTTP server. - Add a development script that uses
nodemonwithts-node. - Configure the watcher to monitor
.tsfiles in thesrcfolder. - Ignore build output folders such as
dist. - Verify that changing the response text restarts the server automatically.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular Dependency Injection: Fix "Can't Resolve All Parameters for Component" Errors
Learn why Angular shows "Can't resolve all parameters for component" and how to fix service injection issues in components.