Skip to content
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

Allow require to load file-type as ES Module #736

Closed
wants to merge 1 commit into from

Conversation

Add `module-sync` in exports, exposing entry points to require/ESM loading.
@Borewit Borewit added the improvement Improvement of existing functionality label Feb 9, 2025
@Borewit Borewit self-assigned this Feb 9, 2025
@imaksp
Copy link

imaksp commented Feb 12, 2025

good, waiting for this, require esm support will also land in Node 20 LTS soon.

@Borewit
Copy link
Collaborator Author

Borewit commented Feb 17, 2025

As pointed out here: #737 (comment)

At least 9 out of 10 of our users are still CommonJS users

Looking at the statistics of the last 7 days, 89.2% of the downloads are still CommonJS and just 10.8% are ESM versions. That tells me, despite ESM is around for many years, there is still a significant demand from CommonJS projects.

The actual number of CommonJS dependent projects / users is even higher, as CommonJS projects which are already importing file-type as an ESM module are counted as ESM downloaders.

With this PR we likely closing the gap for many, as there is clear bottleneck on the latest CommonJS version, which is still the most frequently downloaded version by far.

@Borewit Borewit mentioned this pull request Feb 20, 2025
@Borewit Borewit force-pushed the exports-use-module-sync branch from 083ed1c to 6cc7408 Compare February 21, 2025 09:34
@Borewit Borewit requested a review from sindresorhus February 21, 2025 09:44
@Borewit
Copy link
Collaborator Author

Borewit commented Feb 21, 2025

@sindresorhus, in reply to #741 (comment)

We can use await import inside the function.

This is a misunderstanding Sindre. It is certainly also possible to use dynamic imports with "module-sync". The restriction is:

The format is expected to be ES modules that does not contain top-level await in its module graph

So this is not allowed:

// ESM Entry point file

// Top level await
export quoteOfTheDay = await fetch('https://www.forbes.com/forbesapi/thought/uri.json?enrich=true&query=1&relatedlimit=1');

But this is totally fine:

// ESM Entry point file

export async function detect(buffer {
  const {fileTypeFromBuffer} = await import('file-type'); // dynamic import
  return fileTypeFromBuffer(buffer );
} 

@sindresorhus
Copy link
Owner

This is a misunderstanding Sindre.

That is completely unrelated to this issue. There I'm talking about using await import to be able to import node:fs.

@Borewit
Copy link
Collaborator Author

Borewit commented Feb 25, 2025

Merged with #741, part of v20.3.0

@Borewit Borewit closed this Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants