Solution 1 : Using Nodemon with ts-node
Step 1: Install Required Packages
npm install ts-node nodemon typescript --save-dev
Step 2: Create a nodemon.json Configuration File
Add a nodemon.json file to your project root with the following content:
{
"watch": ["src"], // Directory or files to watch
"ext": "ts", // File extensions to watch
"ignore": ["dist"], // Directories to ignore
"exec": "ts-node ./src/index.ts" // Command to execute
}
Step 3: Run nodemon
Use nodemon to start your application:
npx nodemon
Or
"scripts": {
"start": "npx nodemon"
}
Alternative if we dont want to create a nodemon.json file then we can execute following command directly or we can add it to package. Json.
npx nodemon --watch src --ext ts --exec "ts-node ./src/index.ts"
Pros:-
- Mature and Flexible:
- nodemon offers a wide range of configuration options, such as ignoring files, specifying extensions, and handling custom scripts.
- You can fine-tune what to watch or ignore, making it suitable for larger projects.
- Compatible with Complex Workflows:
- Works well with tools like preprocessors or additional build steps.
- Can integrate with custom tasks (e.g., linting or running multiple commands).
- Community Support:
- Widely used, so you’ll find plenty of resources, plugins, and fixes for common problems.
Cons:-
- Dependency Overhead:
- Requires installing nodemon and configuring it, which adds extra setup and dependencies.
- Performance:
- Slightly slower for very large projects since it monitors a broader range of files.
Solution 2 : Using Node.js’s Built-in –watch Mode
We can use Node.js’s built-in –watch mode which monitors files for changes and restarts your application automatically. This feature is available from Node.js v18.11.0 and later.
We can use this command to run ts-node directly using node js built in watch mode.
node --watch -r ts-node/register ./src/index.ts
Pros:-
- No need to install additional tools like nodemon. Ideal for smaller projects or minimal setups.
- Built-In Simplicity:
- Reduces dependency management.
-
-
- The –watch mode is easy to enable with no extra configuration.
- Good for Small-to-Medium Projects:
-
- Works well for straightforward applications without a lot of custom requirements.
Cons:-
- You can’t exclude specific file types or directories unless you set environment variables (NODE_WATCH_IGNORE).
- No support for additional workflows like running custom scripts.
-
-
- Limited to restarting the application when files change. It lacks advanced capabilities like conditional restart logic or task runners.
Recommendation
- Use nodemon with ts-node if:
- You need advanced features like ignoring specific files or directories.
- You work on large or complex projects with custom workflows.
- You prefer a mature and well-supported solution.
- Use Node.js Watch Mode if:
- You prefer simplicity and fewer dependencies.
- You’re working on a smaller project or want a lightweight setup.
- You’re exploring Node.js’s built-in capabilities for a minimalist approach.