-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module #535
Comments
Duplicate of #525 |
To import a ES module from a CommonJs module wiritten in TypeScript, is very tricky. Have look to this artice How to Correctly Use TypeScript Module Import Syntax and Settings in Various Circumstances, check When a CommonJS module imports an ES Module. (I have to say, I did followed this guidance myself yet). Or use a quick and dirty workaround: /**
* Import 'file-type' ES-Module in CommonJS Node.js module
*/
(async () => {
const { fileTypeFromFile } = await (eval('import("file-type")') as Promise<typeof import('file-type')>);
const type = await fileTypeFromFile('fixture.skp');
console.log(type);
})(); The quick and dirty workaround example can be found in the PR: Borewit/file-type-example#1 If you got a cleaner solution, I like to get your feedback @MichaelGradek. |
I didnt read the error message at first so I tried then realized neither are working
|
and when using the compiled version of the code:
my package.json and tsconfig.json are available at the link: github.com/[...]/package.json |
I can not do any of the different methods I am aware of: Still getting:
|
Stupid updates |
It makes me super annoying!!!!!! |
Thanks @Borewit, I'm using your workaround for now, but somehow I keep getting syntax errors from await not being on top, even tough it is, so I have to wrap everything in an async function. |
I really want to use your npm in my TypeScript project and tried import and require, none of them seem to work. The error message is
code: 'ERR_REQUIRE_ESM' Please make it so i can use the npm like with every other by using either import or require |
hahahha, i like this update. |
this makes me happy |
So that we can avoid the dynamic import workaround monstrosity (sindresorhus/file-type#535)
I had to make the same thing @dedeardiansya told, downgrade. |
@netojose to which version do you downgrade? |
The 17.0.0 is breaking everything. The versions before should work:
|
thanks |
Note that the function const { fromFile } = require('file-type') |
TypeScript now fully supports ESM: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#ecmascript-module-support-in-node-js |
This is still not working... have tried multiple things in my loopback project. I really need the latest fix but unable to do so. @sindresorhus @Borewit pls help |
Are there any plans to make file-type CommonJS-compatible again? Are there any maintained alternatives out there that support getting the mime-type from a buffer like |
Same issue in LoopBack Project |
@T-vK @vaibhavkumar-sf We have managed to use it like this:- `const {fileTypeFromBuffer} = await (eval('import("file-type")') as Promise<typeof import('file-type')>); const fileTrueType = await fileTypeFromBuffer(request);` Using @Borewit example code:- https://github.com/Borewit/file-type-example/pull/1/files |
@prernagp Thank you, I had to modify it a bit, but it basically works.
The only issue is that I can't use await at the top-level... Edit:
It makes sure the import is only called once. |
this version has an |
@vtereshyn , thanks for share this security information. So, we need to choose between the both options (make necessary changes to project to became full ESM, or accept the security issue :( ) |
Yes. But I have huge project written in Typescript and I don't have an ability to change everything just now. That's why I am working on fork of this repo so I can use |
That's it! Thanks! |
Downgrade to version 16 |
My two cents here, upgrading to yarn 3 worked for me! |
I have the same issue, had to downgrade to v16.5.4 to make it work |
Thanks to @Borewit - it helped 🙌 |
Did anyone fork this package with prober build step already? |
I just installed the library onto my TypeScript project and I get the following error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/mike/project/node_modules/file-type/index.js
require() of ES modules is not supported.
Any idea why this may be happening?
"file-type": "^17.1.1"
The text was updated successfully, but these errors were encountered: