-
-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" in Node v21.5.0 #2100
Comments
Getting same issue, but I have a working version that I solved this issue on, and now a new project on which it does not. It is something else and I cannot remember what it is. Will report back once I have debugged the issue. |
Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- NOTE: I have just found this does not seem to support imports !!! |
Could you please provide a minimal example of what does not work and then we can try to figure out why ! |
This is not an ESM package. Try setting type to module in your package.json. |
AFAICS I am not sure ts-node supports ESM. Could you provide the example that works on node v19.0.0, but not v21.5.0. Or is it as simple as setting on v21.5.0 resulting in :-
|
Adding :-
and tsconfig.json :-
We have :- https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader which gives :-
NOTE: I have just found this does not seem to support imports !!! |
I've modified the project a bit and created new repository.link: https://github.com/cibilex/ts-node-err This project doesn't work in node v21.5.0 while works In node v16.20.2 for me. |
You modified start to : - The version I posted last works AFAICT without proper testing, but I did do an Note: https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader NOTE: I have just found this does not seem to support imports !!! |
@cibilex - Did you manage to add the two commits to a ts-node fork or did you just patch the diff's ? |
You can just add |
@cibilex - Wow great thanks, think I was over complicating things :) |
Guys no need for
|
Is there an update on the progress of this bug? |
To summarize: Looks like this has been there for a while. Adding The workarounds I've found so far:
If you are using webpack in your project, this also breaks using Typescript config files. The only workaround for this are:
|
Odd-numbered versions of node are typically beta versions and should not be supported |
I realised that the issue that I had was not with the version of node or ts-node but I was importing a typescript dependancy incorrectly from a library. removing the dependency i was importing and importing the .js from the library rather that a typescript file fixed this issues. |
Update: The node version where ts-node with package.json Simple repro - francip/ts-node-test |
I use node v20.10.0. In Package.json apply this configuration:
In tsconfig.json apply the following:
Hope It will work !! |
@djomajeff - building a repo following your instructions I am getting :- AaronNGray/ts-node-esm-test#1 I added you to the repo. |
@cibilex https://github.com/AaronNGray/ts-node-esm-test works fine now ! |
One of the reasons for the failures with ts-node in ESM projects is linked to the file extensions in import statements. To understand the problem, let's envision this directory structure: .
├── node_modules
├── src
│ ├── sum.ts
│ └── index.ts
├── package.json
├── package-lock.json
└── tsconfig.json In ESM, you must use the import { sum } from './sum.js'; // ...but in reality, the actual file ends in ".ts"
const r = sum(55, 66);
console.log('value:', r); Thus, when the original ts-node loader receives that import
I have created a module called @bleed-believer/path-alias (with ts-node as a dependency). Initially created to resolve path aliases, it now also solves this issue. To execute TypeScript files in ESM directly in Node 20 and above: # Install the library:
npm i --save @bleed-believer/path-alias
# Execute your code using the command line:
npx bb-path-alias ./src/index.ts
# ...or if you need to execute using the node executable:
node --import @bleed-believer/path-alias ./src/index.ts |
@sleep-written Both ECMA 2025 and ECMA 2020 do not specify explicit extension, and the only import examples I found in both ( ECMA 2025, ECMA 2020 ) do not include any extension. But that's neither here, nor there... If you look at my example, you will see nowhere do I use import statement, and the failure happens with a single file project. This is a basic scenario where ts-node should work without extra intermediaries. Though, for folks who are just looking for a quick workaround, your solution might be good. |
Hi @francip, your example is relevant for Node.js versions 18 and earlier. However, when using Node.js version 20 or above with the command: node --loader ts-node/esm ./src/index.ts ...you might encounter the following output:
To address this, consider creating a import { pathToFileURL } from "node:url";
import { register } from "node:module";
register("ts-node/esm", pathToFileURL("./")); And then launch your application with: node --import ./ts-node.register.mjs ./src/index.ts This approach should seamlessly solve the issue, unless your project uses relative imports. According to the Node.js
More details of this problem are explained in this comment. |
Thank you for this fix. It works perfectly on my side. |
Would someone please be able to confirm whether a fix is inbound for node 20.13 and tsnode 10.9, or whether I should be applying one of these workaroundy fixes? |
Somehow, I was able to get this running. I’m not sure what exactly fixed it, but here is my setup. This is so tiresome after returning to programming in TS after a couple of years out, thanks to the articles that helped ([link1]. (https://dev.to/_blank/conquering-the-villains-a-journey-of-setting-up-nodejs-with-express-and-typescript-36ck)). Some pice of code can be redundant !!! package.json
tsconfig.json
nodemon.json
|
I've spent hours if not days trying to make ts-node import my ts files. |
TypeScript ecosystem is as bad as it gets! I'm done. I'm moving everything to JS. It doesn't make any sense to try and use a tool that only gives you headaches and prevents you from using libraries. |
This is no longer an issue and has all been solved plus theres a PR going
through then process.
…On Tue, 25 Jun 2024 at 22:57, Rodrigo Gomes da Silva < ***@***.***> wrote:
TypeScript ecosystem is as bad as it gets!
I'm done. I'm moving everything to JS. It doesn't make any sense to try
and use a tool that only gives you headaches and prevents you from using
libraries.
—
Reply to this email directly, view it on GitHub
<#2100 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAU4EZANEK462XIIPAQ23TZJHRTLAVCNFSM6AAAAABBQ72D62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJQGAZTQOBXGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Aaron Gray - @***@***.***
Independent Open Source Software Engineer, Computer Language Researcher,
Information Theorist, and Computer Scientist.
|
@AaronNGray Not sure when it was resolved, I've spent 2 days trying different combinations of configuration and imports with both CommonJs and ESM loaders. |
@AaronNGray What version of ts-node is this resolved in? Is there a new way to invoke ts-node that makes it work?
|
@francip - here's a working example :- https://github.com/AaronNGray/node-typescript-and-test-esm-example It provides a Node.js ESM example using ts-node with an import example src/main.ts and a Node Test Runner example too all using latest npm modules. Node: v21.7.1 If you would like to test and verify it I would appreciate it :) |
Yehh, but you still import |
@kolya182 although this looks weird, this is actually most logical way to import, considering ESM requires explicit extension, and TS exists in static time only. Extensionless is not a way anymore, so the only alternative is to have explicit After a consideration, it turns out that this is a bliss that TS just works in such case. You can easily import |
@francip - It an engineering solution, it works, is minimally annoying, not perfect, but at the end of the day we can get on and do work, whch is what really matters. niceties aside. Yes I don't like it either and this should have been sorted out properly long ago. |
@AaronNGray Importing |
Hi @DzmVasileusky no I am hoping there will be a proper solution in the pipeline. This is just an “engineering solution” that will help people out for now so they can get on until a patch that solves this properly comes through. Your welcome to update the docs but it’s also dependent on tsconfig.json settings, and also settings in package.json. |
@AaronNGray I am aware (couple of variations) of the "engineering" solution :-) But as you said it yourself, this is not proper user-friendly solution. When you say
people will tend to believe that the proper solution has been done. |
@francip I believe there is a PR. I have not looked at it thought. Anyway best not to be a perfectionist over having a working solution. |
NOTE: npx ts-node renameFiles.ts not working issue: TypeStrong/ts-node#2100
Facing same issue. |
Use tsx instead of ts-node, which is [broken on Node >20](TypeStrong/ts-node#2100). Before: ``` $ npm run sync-figma-to-tokens > sync-figma-to-tokens > ts-node-esm -P tsconfig.json src/sync_figma_to_tokens.ts TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/jyang/figma/variables-github-action-example/src/sync_figma_to_tokens.ts at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9) at defaultGetFormat (node:internal/modules/esm/get_format:203:36) at defaultLoad (node:internal/modules/esm/load:143:22) at async nextLoad (node:internal/modules/esm/hooks:866:22) at async nextLoad (node:internal/modules/esm/hooks:866:22) at async Hooks.load (node:internal/modules/esm/hooks:449:20) at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) { code: 'ERR_UNKNOWN_FILE_EXTENSION' ``` After: ``` $ npm run sync-figma-to-tokens > sync-figma-to-tokens > tsx src/sync_figma_to_tokens.ts Wrote Primitives — Completed.Modern Theme.json Wrote Primitives — Completed.Brutal Theme.json Wrote Tokens — Completed.Light.json Wrote Tokens — Completed.Dark.json Wrote Product interactions — Completed.Default.json ✅ Tokens files have been written to the tokens_new directory ```
Same. The quick fix I got from above which is working for me at the moment is: |
fixed for me with https://github.com/privatenumber/tsx |
Changed from ts-node to node with ts-node/esm as the loader. There is a bug with ts-node that breaks things when using ESM style modules only. see: TypeStrong/ts-node#2100
This comment was marked as abuse.
This comment was marked as abuse.
With node v2x, running tests results in error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" See TypeStrong/ts-node#2100 Solved as per TypeStrong/ts-node#2100 (comment)
First of all ,I already looked at #1997 and most of other websites to solve the problem but I couldn't.This error is throwed when I try to use in Node v21.5.0.I realized that The problem is node version,when I updated Node version to v19.0.0 it works.Could you please update ts-node to work in new Node versions.
Best regards.
The text was updated successfully, but these errors were encountered: