Stop 04 · JavaScript & TypeScript for BackendTopics · 11/12

ts-node

Part of the Backend Developer Roadmap

In short

Want to run a .ts file right now without a build step? ts-node and its faster cousin tsx do exactly that, which makes scripts and quick experiments painless.

A tool that lets you run TypeScript files directly in Node during development, without a separate compile step — speeds up the local dev loop significantly.

The mascot pours hot water into a cup of instant noodles beside an unused cooking pot.

Think of it like this

Instant noodles vs a full dinner

A proper dinner needs chopping, cooking and plating. Instant noodles need hot water and two minutes. ts-node is the instant noodles of TypeScript: pour in your .ts file and eat. Great for a quick bite, not for a wedding feast.

Watch it happen

Run TypeScript directly or build first

ts-node / tsx

  • Compiles in memory on each run
  • No dist folder, instant feedback
  • Best for dev and one-off scripts

tsc then node

  • Compiles once to dist/
  • Fastest startup in production
  • No TypeScript tools needed on server

Use ts-node or tsx while developing. Ship the tsc build to production.

The big ideas

What you need to know

Runs .ts directly

npx ts-node script.ts compiles in memory and runs it. No separate build step, no dist folder.

ts-node can type check

By default ts-node checks types as it runs. --transpile-only skips the check for speed.

tsx is the popular alternative

tsx strips types without checking, starts fast and handles ESM smoothly. Many new projects use it.

Perfect for scripts

Seed a database or run a migration with npx tsx scripts/seed.ts, all in TypeScript.

See it in code

Walk through the code

bash
1npm i -D ts-node typescript @types/node2 3# Run once, with type checking4npx ts-node src/scripts/seed.ts5 6# Run faster, skipping the type check7npx ts-node --transpile-only src/scripts/seed.ts8 9# Popular alternative: tsx (no type check, fast, ESM-friendly)10npx tsx src/scripts/seed.ts11npx tsx watch src/index.ts   # restart on every save

1/4Install ts-node and TypeScript as dev dependencies.

Because tsx skips type checks, run npx tsc --noEmit in CI or before pushing so type errors still get caught.

Save yourself the pain

Common mistakes

Oops

Using tsx and assuming type errors will stop the script.

Do this instead

tsx only strips types. Run tsc --noEmit separately to catch type errors.

Oops

Fighting ts-node ESM errors for an hour on a "type": "module" project.

Do this instead

Try tsx, which handles ESM projects with no extra config, or check ts-node's ESM docs.

Your mission

Write a seed script

~15 min

Quick check

You run a file with tsx that has a type error but valid JavaScript underneath. What happens?

Go deeper

  1. 1Run a `.ts` file directly with `ts-node` instead of compiling first.
  2. 2Set up `nodemon` + `ts-node` for auto-restarting on file changes during development.
  3. 3Learn `tsx` as a faster modern alternative to `ts-node` worth comparing.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge