- Firstly let's automate running the
app.ts
file using this script command by adding"start": "tsc app.ts && node app.js"
. - Then execute the command
tsc --init
to initialize tsconfig.json file - Previously we've used a trick to compile and run the ts code using script now we're going to do it in a separate way.
- Create 2 folders
server
anddist
. Then moveapp.ts
toserver
folder - Please uncomment the
outDir
property and put"./dist""
- Just run
tsc
command in the console and see whether.js
file is created insidedist
folder. - Then run
npm i @types/node -D
to addfs
file sync as dependency and show js code generated for ES version. - Change the
"module": "ES2022"
and see the result - Always we have to run
tsc
and thennode dist/app.js
so let's define a script for this"start": "tsc && node dist/app.js"
- However, we have a simplified way to do this as well
- Then please use
npm install ts-node -D
to install ts-node. - Then replace start script to
"start": "ts-node server/app.ts"
- So, you'll not need the dist directory anymore.