Skip to content
Nilay Kabariya

Entry No.012·Fixes·Updated ·5 min read

Unknown file extension ".ts" (ERR_UNKNOWN_FILE_EXTENSION)

Node refuses to run your TypeScript file. The version where this stops happening, what ts-node has to do with it, and the one-line way to run .ts today.

What changed on Nov 8, 2026: Re-ran on Node 20 to 26 and added TypeScript packages in node_modules: installed ones fail with a new error, workspace links work.

The error, verbatim

Jump to the fix ↓
node:internal/modules/esm/get_format:189
  throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath);
        ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\project\src\index.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:189:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:232:36)
    at defaultLoad (node:internal/modules/esm/load:145:22) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
✓ Reproduced on Windows 11 Pro

The fix that worked

On Node 22.18 or newer, just run it. Older:

npx tsx src/index.ts
Not it? Every cause, tested ↓

Tested on

Node
20.19.4, 22.5.1, 22.6.0, 22.18.0, 24.14.0; 20.20.2, 22.17.1, 22.18.0, 24.22.2, 26.13.0 (Linux, November 8)
ts-node
10.9.2
tsx
4.23.15
TypeScript
5.9.3 and 7.0.2
OS
Windows 11 Pro

✓ Last re-run on GitHub Actions · see the run · all repros

The fix

Run your Node version first, because this error disappears on its own from Node 22.18:

node --version

On Node 22.18 or newer (including Node 24), run the file directly. No loader, no flag, no ts-node:

node src/index.ts

On anything older, install tsx and run the file through it:

npm i -D tsx
npx tsx src/index.ts

For an npm script, or where you need Node’s own flags, load tsx into Node instead:

{ "scripts": { "dev": "node --import tsx src/index.ts" } }

Which Node version does what

I ran the same file, unchanged, on six Node versions:

Node node src/index.ts With --experimental-strip-types
20.19.4 ERR_UNKNOWN_FILE_EXTENSION flag doesn’t exist
22.5.1 ERR_UNKNOWN_FILE_EXTENSION bad option
22.6.0 ERR_UNKNOWN_FILE_EXTENSION runs
22.18.0 runs runs
24.14.0 runs runs
26.11.1 runs runs

One flag did change on 26: --experimental-transform-types was removed. The rest of the 24 to 26 changes are in Node 26 vs Node 24, tested.

So there are three eras, and the answer you find online depends entirely on which one it was written in: before 22.6 Node could not load a .ts file at all, from 22.6 it could behind a flag, and from 22.18 it does it by default.

Why it happens

Node decides how to load a file from its extension. In an ES module project, the list is short: .js, .mjs, .cjs, .json, .node and, on new enough versions, .ts, .mts and .cts. Anything else is refused by name before a single line is read, which is why the stack trace points at get_format and never at your code.

Newer Node versions add TypeScript by stripping the types: the type annotations are replaced with whitespace and the result is run as plain JavaScript. Nothing is type-checked and nothing is compiled, which is why it’s fast and also why it can’t handle everything (see the trap below).

Why ts-node hits it too

This is the version most people actually have, because the error arrives from a tool that is supposed to prevent it:

npx ts-node src/index.ts
TypeError: Unknown file extension ".ts" for C:\project\src\index.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:189:9)
  code: 'ERR_UNKNOWN_FILE_EXTENSION'

ts-node hooks into CommonJS loading. The moment your package.json has "type": "module", your .ts file is an ES module, a different loader handles it, and ts-node’s hook is never consulted. Node reaches the extension check and refuses.

Proof it’s the module system and not the file: I removed "type": "module" from package.json and ran the identical command on the identical Node 20, and it printed hello world.

What didn’t work

The trap after the fix

Type stripping is not compilation. Syntax that has to generate JavaScript, like enum and namespace, is refused:

SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is not supported in strip-only mode

That’s Node 24 running a file whose only crime is one enum. The ways past it, all five variants reproduced: TypeScript enum is not supported in strip-only mode.

A .ts package in node_modules: a different error

Once your own .ts files run, a monorepo can still trip over a dependency that ships TypeScript source ("main": "index.ts"). On November 8, 2026 I imported the same small TypeScript package two ways, installed into node_modules and linked through npm workspaces, on five Node releases:

Terminal: on Node 20.20.2 and 22.17.1 everything fails with ERR_UNKNOWN_FILE_EXTENSION; on 22.18.0, 24.22.2 and 26.13.0 the app's own index.ts prints hello world, the copied TypeScript package fails with ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING, and the workspace-linked package prints hi from lib-ts; below, the full message: Stripping types is currently unsupported for files under node_modules.
Node 20 to 26 on GitHub Actions, and the full message from Windows 11. Full log.
  • Copied into node_modules, as a published package would be, it failed on 22.18.0, 24.22.2 and 26.13.0 with ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING: “Stripping types is currently unsupported for files under node_modules”. Node strips types in your code, deliberately not in your dependencies.
  • Linked through npm workspaces, the same package ran on all three. The link points to the package’s real folder outside node_modules, and Node uses that real path, so the rule doesn’t apply.
  • The boundary for your own files is exact: 22.17.1 still threw ERR_UNKNOWN_FILE_EXTENSION, and 22.18.0 ran the file.

For a TypeScript dependency, either keep it as a workspace package, compile it to JavaScript before publishing (main pointing at .js), or run the app with tsx, which loads TypeScript from node_modules too:

npx tsx src/uses-lib.ts

Test runners

  • node --test follows the same rule as node itself: on Node 20 the .ts test file fails, on Node 24 it runs.
  • Mocha 12 strips types itself, so a .ts test ran on Node 20 as well, but the enum file still failed there with ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX.

Run it yourself

The November 8 version matrix is the unknown-file-extension-ts folder in nk-repro (run 42507477762). The original project:

The project I used is on GitHub, so you can trigger the error on your own machine in under a minute:

git clone https://github.com/nils44344/node-ts-error-repros
cd node-ts-error-repros/01-unknown-file-extension
npm install && npm run repro

Setup

A three-line TypeScript file and a package.json with "type": "module", run unchanged on five Node versions installed side by side, with ts-node 10.9.2, tsx 4.23.15, Mocha 12.0.2, and TypeScript 5.9.3 and 7.0.2. Every result above is the actual terminal output, including the failures.

— N.K., end of entry No.012

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.023

    Fix: TypeScript enum is not supported in strip-only mode

    Node's ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX for enum, parameter properties, namespace, export = and import =. Two ways to run it unchanged, both tested.

    > enum Color { Red, Green }

    FIXED4 min
  2. No.014

    TS1294: syntax not allowed with 'erasableSyntaxOnly', fixed

    TS1294: TypeScript rejects your enum, namespace or constructor shorthand. Why new projects turn this on, what each piece becomes, and when to switch it off.

    > src/a.ts(1,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

    FIXED4 min
  3. No.013

    Fix: ERR_MODULE_NOT_FOUND Cannot find module in ES modules

    Node can't find a file that is clearly there. What ES modules changed about relative imports, why TypeScript path aliases break at runtime, and what to change.

    > node:internal/modules/esm/resolve:275

    FIXED4 min

tip: paste the exact error text