TypeScript needs to be compiled into JavaScript before it can be executed in Node.js. Let's check the steps:
Steps to Run TypeScript Files from the Command Line
1. Install TypeScript
First, you need to have TypeScript installed.
To install TypeScript globally (for use in any project):
npm install -g typescript
2. Create a TypeScript File
Create a .ts file, e.g., hello.ts, with some TypeScript code. For example:
// hello.ts
const message: string = "Hello, Edureka!";
console.log(message);
3. Compile TypeScript to JavaScript
Before running the TypeScript file, you need to compile it to JavaScript using the TypeScript compiler (tsc).
To compile a single file, run:
tsc hello.ts
This will generate a app.js file in the same directory.
If you have a tsconfig.json file (which allows for more complex setups and configurations), you can just run:
tsc
This will compile all the TypeScript files according to the configuration in tsconfig.json.
4. Run the Compiled JavaScript File
Once the TypeScript file is compiled into JavaScript, you can run the resulting .js file using Node.js.
node app.js
You should see the output in the terminal, like:
Hello, Edureka!