-
-
Notifications
You must be signed in to change notification settings - Fork 372
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
fix: Fix require(ESM) for Node v22 #723
base: main
Are you sure you want to change the base?
Conversation
Fixes for Node 22: - sindresorhus#696 - sindresorhus#671 - sindresorhus#668 - sindresorhus#661 - sindresorhus#600 - ... and similar Is that correct?
Your PR would mix ESM code with CommonJS. As stated in the dcoumentation We recommend to migrate your project to ESM as well . If you do want to stick with CommonJS for whatever reason, this is the way to import file-type: const { fileTypeFromFile } = await import('file-type'); Which may not work in TypeScript, see load-esm how to resolve that. |
@Borewit Thank you for your reply. I do not think it mixes ESM with CJS. It just adjust loading this package to new Node.js feature. This package remains pure ESM (which is good decisions, by the way). To use load-esm is not an option for many reasons. Such as static dependency analysis, avoiding dynamic imports, code readability, etc. This PR is a correct and official solution for a pure ESM packages for Node 22 and newer. |
What I don't understand, you link to our JavaScript ESM files in the
I am eager to learn, can you provide a link to that official guidance? |
No, I use it in TS and Nest.js. So my code looks like: import { fileTypeFromFile } from 'file-type';
...
private async validateImage(filePath: string): Promise<void> {
const fileType = await fileTypeFromFile(filePath);
... Unfortunately, Nest.js supports only CJS and does not support ESM. Happilly, Node.js solve this issue. But this package does not support it :-( I think this code is much more clearer than what you suggest: import loadEsm from 'load-esm';
...
private async validateImage(filePath: string): Promise<void> {
const { fileTypeFromFile } = loadEsm('file-type');
const fileType = await fileTypeFromFile(filePath);
...
See https://socket.dev/blog/node-js-delivers-first-lts-with-require-esm-enabled We already use many pure ESM packages without anu problem. Such as I just thought that the point of opensource is to contribute to the development of a project if it helps a lot of people. Which in the case of Nest.js it really is. It doesn't break anything for you, and judging by the amount of ignored issues, I'm not the only one who appreciates this. This PR simply adds support for a new Node.js feature. That's all. I'm not picking a fight over ESM or CJS here. We've got everything we can in ESM. I understand the benefits, but Nest.js obviously doesn't. Why to use hacky |
We differentiate in the file-type The typings are exported via The only thing I can derive from the documentation you provided is that, the still experimental feature, require(esm) It is also clear it does not work for file-type (yet). I also believe that the reason for that is indeed the way we export. But that is not because we forgot or should wire |
Thanks for the constructive reply. Fair enought. It's entirely up to you, we'll fork the package if we have to. |
As a side note @mdorda , I do appreciate you are reaching out, trying to make things better for everyone. I totally see the frustration for many users, who just simply want to be focus on using the modules rather then being stuck on interoperability issues between CommonJS / ESM. We are discussing here, not fighting. I value your input and contribution. This is a different discussion, but |
Fixes for Node 22:
Is that correct?