sindresorhus/file-type

Unable to use plugin: Error [ERR_REQUIRE_ESM]: require() of ES Module

jstdk opened this issue ยท 6 comments

jstdk commented

I am unable to use this plugin in my Typescript bot as it throws the following error on import:

import { fileTypeFromBuffer } from 'file-type';
Then

console.log(await fileTypeFromBuffer(bytes));

Leads to

/home/jost/.nvm/versions/node/v18.16.0/lib/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/jost/Code/simp/node_modules/file-type/index.js from /home/joost/Code/simp/models/messages_media.ts not supported.
Instead change the require of index.js in /home/jost/Code/simp/models/messages_media.ts to a dynamic import() which is available in all CommonJS modules.
  

On SO they advice to add the following to package.json, but that breaks my project altogether

"type": "module"

Thanks for any suggestions

"type": "module"

This is required to use this package.

jstdk commented

@sindresorhus, I was a deep rabit hole, but it works now with "type": "module"

Got here and found this issue because I just had to downgrade file-type 2 versions.

I think the usage stats on NPM tell you everything you need to know about how the authoritarian ESM migration is going.

16.5.4 is the last CommonJS version of file-type ๐Ÿคทโ€โ™‚๏ธ

Version Downloads (Last 7 Days) Tag
16.5.4 1,950,260 version-16
18.5.0 167,986 latest

I'm seeing an issue where it will work with Typescript, but not a jest test -- I don't think the right fix there is to set module to true.

npm install file-type@16.5.4

worked for me