DEV Community

100x.crypto πŸš€πŸš€
100x.crypto πŸš€πŸš€

Posted on

Run TypeScript Files in 30 Seconds

If you have a TypeScript script (.ts file) and want to run it quickly without setting up a full build pipeline or compiling anything manually, this guide shows you how to run it in 30 seconds.

Environment: I'm using WSL (Ubuntu) on Windows, but this works the same on Linux and macOS. I also assume you already have Node.js installed.

Step 1: Create a New Project Directory

mkdir my-typescript-project
cd my-typescript-project
npm init -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install tsx

npm install --save-dev tsx
Enter fullscreen mode Exit fullscreen mode

tsx runs TypeScript files directly without compiling to JavaScript.

Step 3: Run Your TypeScript File

npx tsx your-file.ts
Enter fullscreen mode Exit fullscreen mode

That's it.

Example

Let's look at a practical example. Suppose you have a JSON file exported from a Discord chat and want to convert it to Markdown.

data.json:

[
  {
    "thread_id": 1,
    "thread_name": "Example Thread",
    "message_count": 2,
    "messages": [
      {
        "message_id": 1,
        "author_name": "Alice",
        "timestamp": "2025-01-15T10:00:00Z",
        "content": "Hello, world!",
        "attachments": []
      },
      {
        "message_id": 2,
        "author_name": "Bob",
        "timestamp": "2025-01-15T10:05:00Z",
        "content": "Hi there!",
        "attachments": []
      }
    ]
  }
]
Enter fullscreen mode Exit fullscreen mode

convert-to-markdown.ts:

import * as fs from 'fs';
import data from './data.json';

interface Message {
  message_id: number;
  author_name: string;
  timestamp: string;
  content: string;
  attachments: any[];
}

interface Thread {
  thread_id: number;
  thread_name: string;
  message_count: number;
  messages: Message[];
}

const threads: Thread[] = data as Thread[];
let markdown = '# Threads\n\n';

threads.forEach((thread) => {
  markdown += `## ${thread.thread_name}\n\n`;

  thread.messages.forEach((message) => {
    const date = new Date(message.timestamp).toLocaleString();
    markdown += `### ${message.author_name} - ${date}\n\n`;
    markdown += `${message.content}\n\n`;
    markdown += '---\n\n';
  });
});

fs.writeFileSync('output.md', markdown);
console.log('Converted to output.md');
Enter fullscreen mode Exit fullscreen mode

Set up a project directory as described earlier and place both files inside it.

Then run:

npx tsx convert-to-markdown.ts
Enter fullscreen mode Exit fullscreen mode

output.md is created.

Quick reference

# 1) Setup
mkdir my-typescript-project
cd my-typescript-project
npm init -y
npm install --save-dev tsx

# 2) Add the .ts file into the directory

# 3) Run
npx tsx your-file.ts
Enter fullscreen mode Exit fullscreen mode

Top comments (0)