- Published on
Day03: TypeScript Made Easy with Automatic Compilation Using tsc + nodemon
- Authors

- Name
- irisjustdoit
- @irisjustdoit
Day03: TypeScript Made Easy with Automatic Compilation Using tsc + nodemon
In the previous article, I mentioned that we have to manually run tsc xx.ts and node xx.js every time we write. Isn't that tiring? I'm tired, so I'm sharing this article to help us automate compilation and speed up our learning time.
Create two folders:
srcanddist.In the
srcfolder, create a file namedindex.tsand write:
const index: string = "this's index"
console.log(index)
- The folder structure will look like this:
project
└───dist
│ │
│
└───src
│ index.ts
- Initialize the project in the folder by running the npm initialization command:
npm init -y
- Run the following command to generate the
tsconfig.jsonfile, which is the TS initialization configuration file:
tsc --init
- Open
tsconfig.jsonand set the paths foroutDirandrootDir.
"outDir": "./dist", // Path where the compiled JS files will be generated
"rootDir": "./src", // Path representing the entry point for TS files
- Run
tsc, and you will see the compiledindex.jsin thedistfolder.
tsc
- Run the following command to install
nodemonandconcurrently:
npm i nodemon concurrently --save-dev
- Modify the
scriptssection inpackage.json:
"scripts": {
"start:build": "tsc -w", // Watch TS files in src and automatically compile JS files
"start:run": "nodemon ./dist/index.js", // Execute when a new JS file is generated
"start": "concurrently npm:start:*" // Execute all commands containing "start" (including start:build and start:run)
},
- Run:
npm start
- At this point, you should see the compiled JS file in the
distfolder, and the terminal should printthis's index, indicating successful execution.
project
│ node_modules
│ package-lock.json
│ package.json
│ tsconfig.json
│
└───dist
│ │ index.js
│
└───src
│ index.ts

Isn't it convenient? You can check the demo here, which I have also synchronized on GitHub.
In this article, we set the
outDirandrootDirintsconfig.json. In the next article, I will note what other settings can be configured intsconfig.json. I initially found it confusing when I saw the handbook introducing types, so I decided to move this topic up for better understanding later.
References
https://www.typescripttutorial.net/typescript-tutorial/nodejs-typescript/
