tsm: TypeScript Module Loader

2.3.0 · active · verified Sun Apr 19

tsm is a TypeScript Module Loader for Node.js, currently at version 2.3.0, enabling seamless execution of TypeScript and modern JavaScript files directly within Node.js environments without a separate compilation step. It leverages `esbuild` for high-performance transformations, allowing developers to run `.ts`, `.tsx`, `.mts`, and `.cts` files out-of-the-box. The package is actively maintained, with recent releases addressing compatibility with newer Node.js versions and `esbuild` updates, indicating a responsive release cadence. Its key differentiators include comprehensive support for multiple Node.js execution paradigms: direct CLI invocation (`tsm script.ts`), the `--require` hook for CommonJS environments, and the experimental `--loader` hook for ECMAScript Modules. tsm also handles complex interop scenarios, such as requiring ESM `.js` files, and supports modern TypeScript features like the `satisfies` operator, making it a robust solution for developing Node.js applications with TypeScript by simplifying the developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to start a simple HTTP server written in TypeScript using `tsm` for direct execution, showcasing basic setup and usage via CLI.

// src/server.ts
import { createServer } from 'http';

const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;

createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end(`Hello from tsm-powered Node.js server!\nRequest Path: ${req.url}\n`);
}).listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  console.log('Try visiting /test or any other path.');
});

// To run this server:
// 1. Install tsm: npm install --save-dev tsm
// 2. Add a script to your package.json: "start": "tsm src/server.ts"
// 3. Run: npm start
// Alternatively, run directly: npx tsm src/server.ts

view raw JSON →