tsx is the fastest way to run TypeScript in Node.js. No tsconfig. No build step. Just tsx file.ts and it runs.
Run TypeScript Instantly
# Run any .ts file
tsx server.ts
# Run .tsx files
tsx app.tsx
# Watch mode — auto-restart on changes
tsx watch server.ts
# Run with Node.js flags
tsx --inspect server.ts
tsx --env-file=.env server.ts
Why tsx Over ts-node?
| Feature | tsx | ts-node |
|---|---|---|
| Startup | Instant (esbuild) | Slow (tsc) |
| Config needed | None | tsconfig.json |
| ESM support | Works | Complicated |
| CJS support | Works | Works |
| Watch mode | Built-in | Needs nodemon |
| Path aliases | Automatic | Needs config |
Use Cases
Scripts
// scripts/seed-db.ts
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const products = [
{ title: "Widget", price: 29.99, url: "https://example.com/widget" },
{ title: "Gadget", price: 49.99, url: "https://example.com/gadget" },
];
for (const product of products) {
await prisma.product.create({ data: product });
}
console.log(`Seeded ${products.length} products`);
await prisma.$disconnect();
tsx scripts/seed-db.ts
Quick API Server
// server.ts
import express from "express";
const app = express();
app.get("/api/health", (req, res) => res.json({ status: "ok" }));
app.listen(3000, () => console.log("Running on :3000"));
tsx watch server.ts
Data Processing
// process.ts
import { readFileSync, writeFileSync } from "fs";
import { parse } from "csv-parse/sync";
const csv = readFileSync("data.csv", "utf-8");
const records: { title: string; price: string; url: string }[] = parse(csv, { columns: true });
const processed = records
.filter(r => parseFloat(r.price) < 50)
.map(r => ({ ...r, price: parseFloat(r.price) }));
writeFileSync("output.json", JSON.stringify(processed, null, 2));
console.log(`Processed ${processed.length} records`);
package.json Scripts
{
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "tsx src/index.ts",
"seed": "tsx scripts/seed.ts",
"migrate": "tsx scripts/migrate.ts",
"test": "tsx --test src/**/*.test.ts"
}
}
Node.js Loader (Alternative)
# Register tsx as a Node.js loader
node --import tsx src/index.ts
# Or in package.json
"scripts": {
"start": "node --import tsx src/index.ts"
}
This is useful when you need full Node.js flag control.
Run scraping scripts instantly? My Apify tools + tsx = rapid prototyping.
Custom TypeScript solution? Email spinov001@gmail.com
Top comments (0)