Nodemon is a useful tool to automatically restart your Node.js server when changing files. It’s a perfect helper during development. You can also use Nodemon with TypeScript instead of JavaScript.
This tutorial walks you through the seamless setup and shows you how to restart your Node.js code written with TypeScript using Nodemon.
TypeScript Series Overview
- How to Export Transpiled Code From package.json
- Use Nodemon to Restart Your Server on Changes
- How to Allow Trailing Commas (Comma-Dangle) With Typescript-ESLint
- Use SWC to Speed up TypeScript Code
- Fixing Null Type Ignored in Union Type
- How to Remove Index Signature from a Type
- Module Augmentation Overwrites Declarations Instead of Merging Them
- Get All Keys of an Enum
- Get All Values of an Enum
- Using a String as Enum Key
- Understanding “keyof typeof”
- Get Type of Class Properties and Omit Methods
Restart Your Server When Changing Your TypeScript Code
First, add nodemon
and ts-node
as devDependencies to your project. Adding them as dependencies allow you to use both executables in your project without having them installed globally on your machine:
npm i -D nodemon ts-node
The ts-node package allows you to run your TypeScript code directly without compiling it to JavaScript. It’s like the node
executable but for .ts
files.
Then, create an NPM script combining the nodemon
and ts-node
executables to run your Node.js server written with TypeScript:
{
"scripts": {
"dev:server": "nodemon --watch './**/*.ts' --exec 'ts-node' server.ts"
}
}
The sample dev:server
script from above watches for changes in .ts
files. Customize the watcher to other file extensions when needed.
That’s it! Enjoy a comfortable development experience!
Mentioned Resources
- ts-node repository on GitHub