How to run TypeScript files from command line? How to run TypeScript files from command line? typescript typescript

How to run TypeScript files from command line?


How do I do the same with Typescript

You can leave tsc running in watch mode using tsc -w -p . and it will generate .js files for you in a live fashion, so you can run node foo.js like normal

TS Node

There is ts-node : https://github.com/TypeStrong/ts-node that will compile the code on the fly and run it through node 🌹

npx ts-node src/foo.ts


Run the below commands and install the required packages globally:

npm install -g ts-node typescript '@types/node'

Now run the following command to execute a typescript file:

ts-node typescript-file.ts


We have following steps:

  1. First you need to install typescript

    npm install -g typescript
  2. Create one file helloworld.ts

    function hello(person){   return "Hello, " + person;}let user = "Aamod Tiwari";const result = hello(user);console.log("Result", result)
  3. Open command prompt and type the following command

    tsc helloworld.ts
  4. Again run the command

    node helloworld.js
  5. Result will display on console