ERR_UNKNOWN_FILE_EXTENSION node module_error ai_generated true

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension '.ts'

ID: node/err-unknown-file-extension

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

Node.js can't handle .ts/.jsx file directly. Need loader or compilation step.

generic

Workarounds

  1. 95% success Use tsx or ts-node for TypeScript execution
    Use tsx or ts-node for TypeScript execution

    Sources: https://nodejs.org/api/typescript.html

  2. 85% success Use --loader flag with ts-node/esm
    node --loader ts-node/esm script.ts

    Sources: https://nodejs.org/api/esm.html#loaders

Dead Ends

Common approaches that don't work:

  1. Rename .ts to .js 80% fail

    Loses TypeScript type checking entirely

  2. Add type:module to package.json 70% fail

    Doesn't help — Node still can't parse TypeScript

Error Chain

Preceded by: