Square CloudSquare Cloud

Best way to run TypeScript in production: tsc, tsx or a bundler

Compiling with tsc and running the JavaScript with node is the most predictable path. tsx, Node's type stripping and a bundler such as esbuild also work in production, as long as tsc --noEmit checks the types first, in CI.

Updated 7 min read

Learning objectives

After reading this article you will be able to:

  • Understand the four ways to run TypeScript in production and what each one does with types
  • Know what Node.js type stripping accepts and what it rejects
  • Pick the right approach for your project, with type checking in the right place
On this page

TypeScript does not run on its own: something has to remove the types before the JavaScript executes. The production question is who does it and when. It can be tsc before the deploy, tsx on every start, Node.js itself or a bundler such as esbuild.

The question is the same whatever the framework (Express, Fastify, NestJS or Hono). What changes between the routes is where type checking happens, how much work is left for startup and which syntax each one accepts, and your code's syntax decides which of them fit.

Which criteria matter?#

  1. Where types are checked. Only tsc checks types. The other three just strip or transform code, so if you pick one of them you need tsc --noEmit as a separate step.
  2. What happens on start. Precompiled JavaScript boots straight away. Transforming at runtime costs time at boot and requires the tool to be installed in production.
  3. Supported syntax. enum, decorators, emitDecoratorMetadata and paths aliases do not behave the same way on every route.
  4. Stack traces. A production error should point at the .ts line, not at a generated file.
  5. What ships to the server. A dist/ folder, the source code plus a tool, or a single file.

How does compiling with tsc and running node dist work?#

tsc is the official TypeScript compiler: it checks types and writes one .js file per .ts file into an output folder. In production you run that JavaScript with node, like any other project.

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "rootDir": "src",
    "outDir": "dist",
    "sourceMap": true,
    "strict": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}
json
{
  "type": "module",
  "scripts": {
    "build": "tsc",
    "start": "node --enable-source-maps dist/index.js",
    "typecheck": "tsc --noEmit"
  }
}

With module: nodenext and "type": "module", relative imports carry the extension of the emitted file (./db.js). The --enable-source-maps flag makes Node use source maps in stack traces, so errors point at the original .ts. The docs warn that it can add latency when your code accesses Error.stack.

Pros: types are checked before the deploy, startup is plain JavaScript and the server needs no TypeScript tooling.

Cons: there is a build step and a dist/ folder to produce and ship. And tsc does not rewrite paths aliases: the TypeScript docs say the option only tells the compiler about a mapping another tool applies at runtime. For aliases that work in Node, use package.json subpath imports, which start with #.

Can you run .ts files directly with tsx?#

tsx is a command that runs TypeScript files on Node.js, transforming each file with esbuild as it loads. There is no build and no dist/:

bash
npx tsx src/index.ts

Pros: the same tool works in development (with watch mode) and in production. It handles CommonJS and ESM and understands the paths aliases in tsconfig.json.

Cons: tsx does not check types; a type error only shows up if tsc --noEmit runs somewhere else. The transform happens at runtime, and tsx has to be installed on the server. Because it uses esbuild, the tsx docs list the same limitations: decorators work when experimentalDecorators is on, but emitDecoratorMetadata is not supported. TypeORM, for example, uses that metadata to infer a column's type, and declaring the type explicitly removes the dependency, as shown in which Node.js ORM to use. If your code depends on the metadata, compile with tsc.

On production use, the tsx FAQ says it depends on your needs and risk tolerance, and suggests checking that your code behaves the same in development and production.

What about Node.js native type stripping?#

Type stripping is the Node.js feature that runs .ts files by replacing type annotations with whitespace, with no compilation and no type checking. In versions where the feature is stable, it is on by default and node src/index.ts works with no flag. Check the "Modules: TypeScript" page in the docs for your version.

The price is accepting only syntax that can be erased:

Syntax or feature With type stripping
Annotations, interface, type, generics Works
Type-only imports Need import type or import { type X }
enum, namespace with runtime code, parameter properties, import x = ... ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX error
Decorators Parser error
tsconfig.json (paths, target) Ignored
.tsx files Not supported
.ts inside node_modules Refused
Import without extension (./db) Must be ./db.ts

The Node docs recommend a tsconfig.json that makes your editor and tsc --noEmit flag whatever Node would reject:

json
{
  "compilerOptions": {
    "noEmit": true,
    "target": "esnext",
    "module": "nodenext",
    "rewriteRelativeImportExtensions": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true
  }
}

With rewriteRelativeImportExtensions, the same code, importing ./db.ts, still compiles with tsc if you switch routes later: the compiler rewrites the extensions to .js.

Pros: no extra dependency and no build step. Because types become whitespace, lines stay in place and stack traces point at the right line without source maps.

Cons: erasable syntax only, no tsconfig aliases, no .tsx and no type checking.

When should you use a bundler like esbuild?#

A bundler combines your code into a single JavaScript file before the deploy. With esbuild:

bash
npx esbuild src/index.ts --bundle --platform=node --format=esm \
  --packages=external --sourcemap --outfile=dist/index.mjs
node --enable-source-maps dist/index.mjs

With --packages=external, dependencies stay out of the bundle and, as the docs say, must be installed when the file runs. esbuild converts enum and namespace to JavaScript and reads paths from tsconfig.json, but it does not check types (its docs tell you to run tsc --noEmit in parallel), does not emit .d.ts files and does not support emitDecoratorMetadata.

tsup, which uses esbuild under the hood, says in its README that it is no longer actively maintained and points to tsdown, which presents itself as a library bundler. For an application, esbuild on its own is usually enough.

Pros: fast builds, one file to ship and support for nearly all TypeScript syntax.

Cons: one more tool to configure, source maps needed for readable stack traces and type checking on the side.

How do the options compare?#

Criterion tsc + node dist tsx Node type stripping esbuild
Checks types Yes No No No
Build step Yes No No Yes
What ships dist/*.js .ts + tsx .ts .js bundle
enum and parameter properties Yes Yes No Yes
emitDecoratorMetadata Yes No No No
paths aliases Not rewritten Yes No Yes

Which one fits each scenario?#

Scenario Recommendation
API or bot where you want the most predictable startup tsc + node --enable-source-maps dist/index.js
Deploy with no build step, source code on the server tsx, with tsc --noEmit in CI
Script or small service that only uses erasable syntax Node type stripping
Code that depends on emitDecoratorMetadata tsc
A single file to ship esbuild with --packages=external
Library published to npm tsc or tsdown, with .d.ts

On every route, type checking has to block the deploy: a CI job running tsc --noEmit, as in automatic deploys with GitHub Actions, does it. Also remember that build tools in devDependencies are left out of a production install, the cause of many npm install errors on deploy. If the build breaks, how to read build errors helps you find the line that matters.

How to do this on Square Cloud#

In Square Cloud's TypeScript runtime, the entry point is the .ts file itself (also .tsx, .mts and .cts), run with tsx, which is already available on the first start. There is no dist/ folder and no tsc --build: you upload the source code, and the package.json dependencies are installed on the server. It is the tsx route from the table, with no tool to set up.

Since tsx does not check types, put tsc --noEmit in a GitHub Actions job and, if it passes, use the official GitHub Action (squarecloudofc/github-action@v2) with the required token input, stored as a repository secret, and command: commit <appId> --restart. From your machine, the CLI does the same with squarecloud commit <appId> --restart.

If you prefer the tsc and node dist route, compiling is still an option: run tsc in CI and upload the output, or put the compilation in the start command. An example to adapt to your project: npx tsc && node dist/index.js, with the typescript package in dependencies. Environment variables live in the dashboard, per application, and apply on the next restart. See the details on the TypeScript runtime page.

Deploy

Push from GitHub, the CLI or a ZIP and go live in seconds

TypeScript hosting

FAQs

Can you use tsx in production?

Yes. The tsx FAQ says the decision depends on your needs and risk tolerance: it transforms code with esbuild and does not check types. Run tsc --noEmit in CI before deploying so a type error never reaches production.

Can Node.js run TypeScript without any tool?

It runs .ts files that only use erasable syntax: Node replaces type annotations with whitespace and runs the rest, without checking types. Enums, namespaces with runtime code, parameter properties and decorators throw errors, and tsconfig.json is ignored.

Do I need the typescript package in production?

Only if tsc runs on the server. If you compile in CI and ship the JavaScript, typescript can stay in devDependencies. If you run with tsx, tsx is what must be installed in production.

Why do tsconfig paths aliases break after tsc?

Because tsc does not rewrite import paths: the docs say paths only tells TypeScript about a mapping another tool applies. Use package.json subpath imports (they start with #) or a tool that resolves the aliases.

tsc or esbuild to compile?

tsc checks types and emits one .js per .ts file. esbuild only transforms the code, fast, and can bundle everything into one file; its own docs tell you to run tsc --noEmit in parallel to check types.

Next articlenpm install failed on deploy: ERESOLVE, node-gyp and Node versionWhen npm install fails on deploy, the first npm error code line names the cause: ERESOLVE, node-gyp, EBADENGINE, lockfile, devDependencies or memory.

More in Deploy

See all 38 articles