Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readNode

Run erasable TypeScript directly in Node

Run a TypeScript utility directly on Node 22, use explicit source extensions and type imports, and keep type checking in a separate command.

You have a small TypeScript utility and want to run it as a script. It uses annotations and imports a type from another file. Node can remove that syntax as it loads the file, so this example can run straight from its source.

Mark the imports Node can erase

Save this as task.mts. It exports a runtime value and the type that describes it.

export type Task = { title: string };
export const task: Task = { title: "Deploy" };

The value export is task. After stripping, Task has no runtime export.

Save the entry point next to it as print.mts. The .mts extension tells Node to load each file as an ES module, regardless of a nearby package.json.

import { task, type Task } from "./task.mts";

function printTask(value: Task): void {
  console.log(value.title);
}

printTask(task);

Node erases the annotations and the type import before it runs the script.

Use the real filename, including .mts, in the import. If you remove type from the named import, Node tries to find a live export named Task and fails to load the module. With a .ts file, Node uses the module rules it applies to .js files. Choose the extension that matches how you want the script loaded.

Run it on the Node 22 line

From Node 22.18, type stripping is on by default. From Node 22.6 through 22.17, use the second command to opt in.

node print.mts
node --experimental-strip-types print.mts

On Node 22.18 or later in that line, both commands print Deploy. Use the first form there.

Node’s type stripping ignores tsconfig.json, including paths aliases. Run tsc --noEmit separately with a config that matches the imports Node will load.

If you see ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX for an enum, that syntax needs a runtime value. See running enums on Node 22 for the choice between a transform and plain JavaScript values.