How to fix TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension .ts
Error Case
Common case TypeError [ERR_UNKNOWN_FILE_EXTENSION]
in typescript ESM with ts-node
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /index.ts at new NodeError (node:internal/errors:371:5) at Object.file: (node:internal/modules/esm/get_format:72:15) at defaultGetFormat (node:internal/modules/esm/get_format:85:38) at defaultLoad (node:internal/modules/esm/load:13:42) at ESMLoader.load (node:internal/modules/esm/loader:303:26) at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58) at new ModuleJob (node:internal/modules/esm/module_job:63:26) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11) at async Promise.all (index 0) at async ESMLoader.import (node:internal/modules/esm/loader:281:24) { code: 'ERR_UNKNOWN_FILE_EXTENSION' }
tsconfig.json
add following codes to tsconfig.json
to fix TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
{
"ts-node": {
"compilerOptions": {
"module": "CommonJS"
},
"esm": true,
"experimentalSpecifierResolution": "node"
}
}
Official Issues
Conclusion
Just adding {"ts-node":{"esm":true}}
will fixing TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"