sindresorhus / file-type

Detect the file type of a file, stream, or data
MIT License
3.64k stars 345 forks source link

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

Closed jstdk closed 1 year ago

jstdk commented 1 year ago

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

sindresorhus commented 1 year ago

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

sindresorhus commented 1 year ago

"type": "module"

This is required to use this package.

jstdk commented 1 year ago

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

nabilfreeman commented 1 year ago

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
wyardley commented 1 year ago

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.

DanielWTE commented 10 months ago

npm install file-type@16.5.4

worked for me