How to run TypeScript files from command line: complete guide
Hi everyone! I'm new to TypeScript and trying to figure out how to run my .ts files from the command line. I wrote a simple script:
```typescript
// hello.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
const message = greet("World");
console.log(message);
```
When I try to run `node hello.ts`, I get an error:
```
SyntaxError: Unexpected token ':'
at wrapSafe (internal/modules/cjs/loader.js:988)
```
I understand that Node.js doesn't understand TypeScript directly. I tried installing ts-node globally and running `ts-node hello.ts` — it worked. But I've heard this isn't the only way. Are there other options? Maybe I need to compile to JavaScript? Or can I configure Node.js to understand TypeScript natively?
I want to understand wha
No answers yet.
Log in to leave an answer