TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" (Node.js) — how to fix
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/me/app/src/index.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
code: 'ERR_UNKNOWN_FILE_EXTENSION'
Quick fix: Easiest: run TypeScript with tsx instead: npx tsx src/index.ts. To keep ts-node in an ESM project, use its ESM loader: node --loader ts-node/esm src/index.ts. Or compile first with npx tsc and run the .js output with node.
What it means
Node itself only executes JavaScript. Tools like ts-node add a hook that compiles .ts files on the fly. ts-node's default hook works with Node's CommonJS loader. If your package.json has:
{ "type": "module" }
then .ts entry files are loaded through the ES module loader, the hook isn't involved, and Node rejects the unknown extension with ERR_UNKNOWN_FILE_EXTENSION.
The same error appears for any extension the ESM loader doesn't know (e.g. .tsx, .coffee) when no loader handles it.
Fix 1: use tsx
tsx runs TypeScript in both ESM and CommonJS projects with no configuration:
npm install --save-dev tsx
npx tsx src/index.ts
In package.json scripts:
{
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "tsx src/index.ts"
}
}
tsx only strips types; it doesn't type-check. Run npx tsc --noEmit separately (e.g. in CI) to catch type errors.
Fix 2: use ts-node's ESM loader
Keep ts-node and register its ESM loader explicitly:
node --loader ts-node/esm src/index.ts
Newer Node versions print an ExperimentalWarning about --loader; the script still runs. You can also enable ESM in tsconfig.json so the ts-node command uses it:
{
"compilerOptions": { "module": "NodeNext", "moduleResolution": "NodeNext" },
"ts-node": { "esm": true }
}
With NodeNext module resolution, relative imports must include the extension as it will be after compilation, e.g. import { x } from "./util.js" even though the file is util.ts.
On Node 20 and later the ts-node command with esm: true has been unreliable and can still throw this error; the node --loader ts-node/esm form above, or tsx, works more consistently.
Fix 3: drop "type": "module"
If you didn't need native ESM and only added "type": "module" to make import syntax work, remove it. TypeScript compiles import statements to CommonJS when "module": "CommonJS" is set in tsconfig.json, and plain ts-node src/index.ts works again.
Fix 4: compile, then run JavaScript
For production, don't run .ts directly. Build once and run the output:
npx tsc # writes JS to outDir, e.g. dist/
node dist/index.js
Make sure the start script points to the .js file in dist/, not to src/index.ts. Pointing node at a .ts file is a common cause of this error in Docker images and on hosting platforms.
Fix 5: Node's built-in type stripping
Recent Node versions can run .ts files directly by stripping type annotations. On Node 22.6 and later it is behind a flag:
node --experimental-strip-types src/index.ts
From Node 23.6 it is enabled by default. It only supports syntax that can be erased (no enum, no namespace with values, no parameter properties without extra flags) and doesn't read tsconfig.json paths, so tsx is still the more forgiving option.
Check your version with node -v.
Last updated 2026-11-30.