Run a Typescript file with Node.Js
To run the TypeScript file from a Windows command line, you need to have Node.js installed on your machine. Here are the steps to run the file:
-
Open a command prompt by pressing the
Windows key+Ron your keyboard, typingcmd, and then pressingEnter. -
Navigate to the directory where your TypeScript file is located using the
cdcommand. For example, if your file is located inC:\Users\YourUsername\Documents\my-project, you can navigate to that directory by typing the following command:cd C:\Users\YourUsername\Documents\my-project -
Use the following command to install the necessary dependencies (if you haven’t already):
npm install typescript @types/node -
Compile your TypeScript file using the following command:
npx tsc your-file-name.tsThis will create a JavaScript file in the same directory as your TypeScript file.
-
Run the JavaScript file using the following command:
node your-file-name.jsThis will execute your TypeScript code and output the result in the console.
Note that you should replace your-file-name with the actual name of your TypeScript file (without the .ts extension) in the commands above.