Lesson 3 of 9 · 23m
TypeScript Execution: tsx, ts-node, Loaders, and Builds
What TypeScript does and does not do at runtime, when on-the-fly execution is acceptable, and how to configure builds that Node can actually load.
A new hire once asked why production “ran TypeScript” but crashed on an enum that typechecked perfectly. The answer is the sentence that clears up half the ecosystem: TypeScript is a compile-time layer. Node executes JavaScript. Everything else — ts-node, tsx, Bun, Deno, strip-types — is a strategy for producing or interpreting that JavaScript at some stage of the pipeline.
Type erasure is absolute
type User = { id: string; admin: boolean };
const user = JSON.parse(input) as User;
if (!user.admin) throw new Error("forbidden");TypeScript does not validate JSON.parse. It deletes the types. The as User cast is not a runtime check; it is you telling the compiler to stop asking questions. If input crosses a process or network boundary, validate it with zod, valibot, JSON Schema, or handwritten guards.
This matters operationally because runtime crashes caused by bad input are often mislabeled type errors. They are validation errors.
Choose the execution model per environment
For local development, on-the-fly transpilation is fine because latency is human-scale:
node --watch --import tsx src/server.tstsx esbuild-transpiles TypeScript quickly and handles modern ESM paths well. ts-node can do more faithful type-aware execution, but it is easier to misconfigure and slower in large projects.
For production, prefer compiled artifacts:
tsc -p tsconfig.build.json
node dist/server.jsCompiling ahead of time makes startup predictable, avoids shipping transformer dependencies, exposes module errors in CI, and lets containers run with a smaller attack surface. The convenience of executing source directly in production is rarely worth the diagnostic ambiguity.
tsconfig is a runtime compatibility contract
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"exactOptionalPropertyTypes": true,
"outDir": "dist",
"rootDir": "src",
"sourceMap": true,
"declaration": false,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}moduleResolution: "NodeNext" is the important modern choice for real Node output. It forces explicit file extensions in ESM imports:
import { createApp } from "./app.js";Yes, the extension is .js even though the source is app.ts. You are writing the specifier that exists after compilation. Teams fight this for a week and then stop fighting because it prevents an entire class of bundler/runtime drift.
Source maps are operational equipment
Without source maps, a production stack trace points at minified or transpiled output. Configure:
node --enable-source-maps dist/server.jsand ship maps where your error collector can access them. They may contain source content depending on settings, so do not blindly publish them as static assets.
Watch modes hide restart behavior
node --watch is excellent locally. It is not a supervisor. It does not implement rolling deploys, crash backoff, health draining, or signal-aware orchestration. Development convenience must not leak into the production process model.
Field notes
skipLibCheckreduces noise but does not validate your own types; keepstricton.--experimental-strip-typesis useful for simple scripts, but it is not a general TypeScript build system.- If decorators, metadata reflection, or enum behavior matter, test the transformer you actually ship — esbuild and
tscare not interchangeable in every corner. - A production image should contain
dist, not your wholesrctree plus every compile-time dependency. - Set
"type": "module"andNodeNexttogether; mixing bundler-style resolution with Node output creates imports that only work by accident.
Was this lesson helpful?