How to run TypeScript files from command line?
Problem:
I am using TypeScript in my project. I am having trouble running the TypeScript files. ES6 has babel-node example.js. You can run node js using node pathToFile.js. How do you run TypeScript using the command line?
In this article, we are going to learn how to run TypeScript files from the command line.
Solution:
There are many ways you can run your TypeScript file. Here are a few best solutions:
1. You can use @type/node. To use this you have to install the package. You can install the package globally using this:
npm install -g ts-node typescript '@types/node'
And to execute the TypeScript file you do this:
ts-node your-typescript-file.ts
2. You can also compile and run your code by using tsc.
macOS/Linux
tsc your-typescript-file.ts && node your-typescript-file.ts
Windows:
tsc your-typescript-file.ts | node your-typescript-file.ts
Thank you for reading this article. If you have any more questions you can comment below.