20

In TypeScript, you'd use the following command to enter watch mode:

tsc -w -p .

Then when tsc does detect a change, it'll transpile the .ts files and generate .js files.

This means that you can run the new JavaScript files using:

node example.js

I'd like to run TypeScript files directly, without having to use node example.js, how can I do this?

1
  • 3
    Why you don't want to run typescript by node example.js? Commented Aug 29, 2016 at 0:28

4 Answers 4

22

will run typescript without node example.js command How can I do it

You can use ts-node to compile + run directly (without ever writing to disk):

ts-node example.ts

More

https://github.com/TypeStrong/ts-node

Sign up to request clarification or add additional context in comments.

2 Comments

It's a good tool to compile typescript without generating js file. Are there any tools that specified in the official microsoft? – user3736174 5 mins ago
how do I delete a message after run? message : "Watching build tasks has finished."
7

After doing npm init, install these packages.

npm install -g ts-node typescript '@types/node'

Then run your files as

ts-node example.ts

5 Comments

This is a duplicate of the basarat's answer.
I have added the necessary packages installation step also. With the other answer, devs might not know ts-node is a npm package and they have to install it before running that command.
The other answer is linking to the repo on GitHub, which is IMO better because it has more information and won't become outdated when the project changes. If you want to add some details to an answer, you could suggest an edit instead of posting a new one :)
I'm aligned on your point. Thanks!
@PavneetKaur don't listen to him, link answers are generally frowned upon, your answer is better even if it is a duplicate
6

I fond tsx may be a better alternative for ts-node.

I have not researched about it deeply enough, but after severl cases, where ts-node couldn't work while tsx could work as expected with no manual configuration, I'm totally using tsx now , like:

npx tsx ./script.ts

More info : tsx readme

Besides, it looks like tsx use esbuild to have a better speed.

Comments

0

Use Bun to run TypeScript directly: https://bun.sh/

quote: Develop, test, run, and bundle JavaScript & TypeScript projects—all with Bun. Bun is an all-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager. Bun aims for 100% Node.js compatibility.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.