ts-node: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
Search Terms
When the node version is 18.20.0, running TS-node --esm **.ts displays TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.ts”. Runs successfully after the Node version is reduced to 16.20.2.
PS D:\Testspace\ts-node-demo> pn dev
> ts-node-demo@1.0.0 dev D:\Testspace\ts-node-demo
> ts-node-esm src/index.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for D:\Testspace\ts-node-demo\src\index.ts
at new NodeError (node:internal/errors:405:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:136:11)
at defaultGetFormat (node:internal/modules/esm/get_format:182:36)
at defaultLoad (node:internal/modules/esm/load:101:20)
at nextLoad (node:internal/modules/esm/hooks:864:28)
at load (D:\Testspace\ts-node-demo\node_modules\.pnpm\ts-node@10.9.2_@types+node@20.14.2_typescript@5.4.5\node_modules\ts-node\dist\child\child-loader.js:19:122)
at nextLoad (node:internal/modules/esm/hooks:864:28)
at Hooks.load (node:internal/modules/esm/hooks:447:26)
at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:786:20) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
ELIFECYCLE Command failed with exit code 1.
PS D:\Testspace\ts-node-demo> nvm list
* 18.20.0 (Currently using 64-bit executable)
16.20.2
PS D:\Testspace\ts-node-demo> nvm use 16.20.2
Now using node v16.20.2 (64-bit)
PS D:\Testspace\ts-node-demo> pn dev
> ts-node-demo@1.0.0 dev D:\Testspace\ts-node-demo
> ts-node-esm src/index.ts
hello world
PS D:\Testspace\ts-node-demo>
Expected Behavior
Actual Behavior
Steps to reproduce the problem
Minimal reproduction
Specifications
- ts-node version: 10.9.2
- node version: 18.20.0
- TypeScript version: 5.4.5
- tsconfig.json, if you’re using one:
{
"compilerOptions": {
"rootDir": "./src",
"target": "es2016",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"verbatimModuleSyntax": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
}
}
- package.json:
{
"type": "module",
"name": "ts-node-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-esm src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
- Operating system and version: Windows 11 Pro 10.0.22631 Build 22631
- If Windows, are you using WSL or WSL2?:
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 25
- Comments: 19
Commits related to this issue
- chore: esm fix for ts-node https://github.com/TypeStrong/ts-node/issues/2122#issuecomment-2350839871 — committed to dragonrealms-phoenix/phoenix by KatoakDR 2 years ago
I had the same issue, downgrade Node to 18.18 help. My engines:
seems to work for me with node and
--loaderflagnode: v20.5.0 ts-node: v10.9.2
try to use
tsxinstead. It works flawlessly for me.Last week ts-node was working without any issues. Now it’s giving me headache. Is there an issue with the latest ts-node and typescript or some incompatibility issue with node and ts-node or what is causing this?
Currently the issue can be reproduced by following these steps:
This will now first throw you an error
Try to fix that by adding the
"type": "module"into package.json. Now runningpnpm exec ts-node main.tsgives you the very annoyingAdding
--esmflag does not change anything as the commandpnpm exec ts-node --esm main.tsalso gives the same error above.Version used in this test:
Thanks @lhapaipai!
In my scenario, I had to also workaround the following error. I think it stems from my project does not have a top-level
tsconfig.json, but rather sub-folders define different configs similar to a monorepo. So I either had to manuallycddown into a sub-folder then run the command, or I could have node figure it out via the import command.My modified command is:
Example Usage:
It runs when I change the type to commonjs in package.json.