Prerequisites
You should have basic knowledge of :
- Typescript syntax
- Interfaces
- types
When developing and testing a server in Nodejs you normally always have to restart your server on every change made to your file. Nodemon is a great and useful tool for restarting your server on every change to your file. This tutorial will introduce you to the setup and shows you how to restart your Nodejs code written with TypeScript using Nodemon.
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: yarn add -D nodemon ts-node typescript
you can also install these packages with npm using: npm install -D nodemon ts-node typescript
The ts-node package allows you to run your TypeScript code directly without compiling it to JavaScript. It is just like the node executable but for typescript files.
Then, create an npm script combining the nodemon and ts-node executables to run your Nodejs server written with TypeScript: "scripts": {
"server": "nodemon --watch './**/*.ts' --exec \"ts-node\" server.ts"
}
The sample server script from above watches for changes in .ts files.
That’s all! Happy hacking!
Top comments (0)