How to run TypeScript files from the command line?
Hi everyone! I just started learning TypeScript and ran into a problem. I wrote a simple script in TypeScript, but I can't run it from the command line. I tried the command `node myfile.ts`, but Node.js gives an error that it can't understand TypeScript syntax.
Here's my code:
```typescript
// hello.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
const message = greet('World');
console.log(message);
```
When I run `node hello.ts`, I get this error:
```
SyntaxError: Unexpected token ':'
at wrapSafe (internal/modules/cjs/loader.js:...)
```
I understand that Node.js doesn't natively support TypeScript, but how do I properly run .ts files? I've heard about ts-node, but I'm not sure how to install and use it. Maybe there are other ways?
No answers yet.
Log in to leave an answer
Blogs
Best IDEs and Tools for Rust Development in 2024