How to run TypeScript files from the command line

0 votes

How to run TypeScript files from the command line?

I'm starting to work with TypeScript and want to execute my .ts files directly from the command line. I'm familiar with JavaScript execution using Node.js, but I'm unsure about the process for TypeScript. How do I set up and run TypeScript files from the terminal efficiently?

Nov 27 in Angular by Nidhi
• 3,520 points
29 views

1 answer to this question.

0 votes

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!

answered Nov 27 by Navya

Related Questions In Angular

0 votes
0 answers

How to change the value of an Observable in TypeScript Angular?

How to change the value of an ...READ MORE

Nov 26 in Angular by Nidhi
• 3,520 points
32 views
0 votes
1 answer

How can we redirect to another page from existing page on clicking alert?

hii, It is really simple to redirect from ...READ MORE

answered Feb 6, 2020 in Angular by Niroj
• 82,840 points
4,294 views
0 votes
1 answer

How to pass a string parameter from angular UI to node.js backend?

Hello Kartik, There are three ways to get ...READ MORE

answered Apr 22, 2020 in Angular by Niroj
• 82,840 points
10,871 views
0 votes
1 answer

How to get parameter value from query string?

Hello @kartik, Using component <Route path="/users/:id" component={UserPage}/> this.props.match.params.id The component is ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
1,689 views
0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
5,909 views
0 votes
2 answers

How to access session variables from any class in ASP.NET?

Accessing session Variable in a class file ...READ MORE

answered Sep 3, 2020 in Angular by Okugbe
• 280 points
7,673 views
0 votes
1 answer

Cannot access web3 object with typescript and ethereum

You still need to instantiate it first. ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,370 points
2,905 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,150 points
1,583 views
0 votes
1 answer
0 votes
1 answer

Can't bind to 'formGroup' since it isn't a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
21,365 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP