TypeScript Support in Node.js

Node.js provides two ways to run TypeScript files: full support via third-party packages, or lightweight built-in support through type stripping. Full support allows all TypeScript features (including tsconfig.json) using tools like tsx. Type stripping, on the other hand, removes inline types without type checking or complex transformations, enabling quick execution of .ts files. Node.js supports both CommonJS and ES modules in TypeScript, with module system determined by file extensions and package.json settings.

TypeScript runtime supporttype strippingimport typesource maps

~2 min read · Updated Dec 30, 2025

1. Enabling TypeScript


  • Full support: Use third-party packages like tsx for complete TypeScript features.
  • Type stripping: Node.js removes erasable TypeScript syntax and executes the file directly.

// Install tsx
npm install --save-dev tsx

// Run TypeScript file
npx tsx your-file.ts

2. Type Stripping


Node.js strips erasable syntax and replaces inline types with whitespace. Features requiring transformation (e.g., enums, parameter properties) need the --experimental-transform-types flag.


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

3. Determining Module System


  • .ts: Determined like .js files, based on type in package.json.
  • .mts: Always ES module.
  • .cts: Always CommonJS.
  • .tsx: Unsupported.

File extensions are mandatory in import and require statements.


4. TypeScript Features


  • Features requiring transformation: enums, namespaces with runtime code, parameter properties, import aliases.
  • Supported: namespaces and modules without runtime code.
  • Unsupported: decorators (parser error).

5. Importing Types


The type keyword is required for type imports:


// Correct
import type { Type1, Type2 } from './module.ts';
import { fn, type FnParams } from './fn.ts';

// Runtime error
import { Type1, Type2 } from './module.ts';
import { fn, FnParams } from './fn.ts';

6. Non-file Inputs


Type stripping works with --eval and STDIN. Module system is determined by --input-type. TypeScript syntax is unsupported in REPL, --check, and inspect.


7. Source Maps


Type stripping does not require source maps. When --experimental-transform-types is enabled, source maps are generated automatically.


8. Limitations


  • Node.js does not execute TypeScript files inside node_modules.
  • tsconfig.json features like paths are unsupported.
  • Closest alternative: subpath imports with # prefix.

Conclusion


TypeScript in Node.js can be enabled either with full support via third-party tools or lightweight type stripping. For development and testing, type stripping is fast and simple, while full support is recommended for complex projects requiring all TypeScript features.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue