TypeScript — Use Nodemon to Restart Your Server on Changes

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

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

Explore the Library

Find interesting tutorials and solutions for your problems.