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.
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/
Related Posts
Day30 :I finally finished!
I finally finished! Recently, I went to draw, and here is a drawing of "The Little Prince and the Fox." My favorite part of "The Little Prince" is their story. Drawing is really therapeutic, just like the feeling of finishing this challenge.
Day29 :TypeScript Learning : React + TypeScript Todo App Part2
Continuing with the Todo app part 2, I will document the issues encountered during the implementation.
Day28 :TypeScript Learning : React + TypeScript Todo App Part1
After taking so many notes, it's finally time to implement a simple to-do app and document the issues encountered during the implementation.