. Advertisement .
..3..
. Advertisement .
..4..
Are you having trouble with the “How to run a TypeScript file from the Command line” problem? If yes, don’t miss this article! We will give you some solutions to solve this problem. Read on it.
What can we do to run a TypeScript file from the Command line?
Solution 1: Launch node filename.js to run the created js file
TypeScript will create a js file with the same name at runtime when you execute tsc filename.ts. Launch node filename.js to run the created js file.
The creation of a TypeScript file named helloworld.ts and some content. To launch helloworld.ts, run the commands shown below.
tsc helloworld.ts
node helloworld.js
It will give you the result:
hello world from typescript file
Solution 2: Utilize the ts-node package
You also can utilize the ts-node package to run a TypeScript file from the Command line.
First, run the following command to install the ts-node package with npm or any other package managers.
npm install -g ts-node
Then, run the TypeScript files with this command:
ts-node filename.ts
Please run the following command to execute the same file that we attempted in solution 1.
ts-node helloworld.ts
It will give you the result:
hello world from typescript file
Solution 3: Combine both commands with a pole | and &&
You also can execute a TypeScript file from the command line by combining both commands with a pole | and &&, as shown below:
For Linux or MacOS:
tsc helloWorld.ts && node helloWorld.js
For Windows:
tsc greet.ts | node greet.js
Conclusion
We hope you enjoy our article about the problem “How to run a TypeScript file from the Command line”. Whenever you have issues or any questions, please leave your comment below. ITtutoria is always here to help you. Thank you for your reading!
Read more
Leave a comment