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

node: prefix error using in browser #502

Closed
AaronPorts opened this issue Dec 2, 2021 · 21 comments · Fixed by #507
Closed

node: prefix error using in browser #502

AaronPorts opened this issue Dec 2, 2021 · 21 comments · Fixed by #507

Comments

@AaronPorts
Copy link

Get the following errors when I try to use fileTypeFromBlob in browser:

ERROR in node:buffer
Module build failed: UnhandledSchemeError: Reading from "node:buffer" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
...
ERROR in node:stream
Module build failed: UnhandledSchemeError: Reading from "node:stream" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
...

I can successfully build my project only with target: "node" in webpack config. Webpack 5.64.4, file-type 17.0.0.

@daveteu
Copy link

daveteu commented Dec 6, 2021

I think version 17 is broken for browser (at least), or server side rendering.

@pastelmind
Copy link

I ran into a similar issue using Vite.js and ended up using magic-bytes.js instead. It's much smaller and doesn't rely on the Node.js stdlib.

@Borewit
Copy link
Collaborator

Borewit commented Dec 9, 2021

Thank you for bringing this issue to our attention.

It is clear that ES-Module introduced some issues using file-type in the browser. Although file-type has been primarily written for Node.js, I would personally like to see it can be easily used by the client side community as well.

One of the aims of ES-Module is improving interoperability, and here we are, it seems now harder then every before.

The dependency on node:Buffer seems to be the challenge. You client side boys and girls always fixed that by polyfilling Node with Webpack.

That pollyfilling seems to slowly move to the trashcan, just like CommonJs modules. Modules must specify there own requirement and not rely on pollyfills. I believe what is missing is that file-type does not declare a dependency on node:Buffer. At the same time, for a Node environment, our primary audience, it is not really great to introduce dependencies already overlapping with the Node.js API.
It would be great if we could depend on buffer only in a browser environment. Similar how we only fileTypeFromFile(path) expose to Node.

I have no magical solution here, I am not super experienced with ES-module, maybe there is a very simple solution. It's an open source project, everyone could pick up this challenge.

Looking forward to hear your thoughts.

@AaronPorts
Copy link
Author

@Borewit thanks for your reply. As a workaround, we can install polyfills and import them by webpack directly:
npm install buffer readable-stream -S
in webpack plugins section:

new webpack.NormalModuleReplacementPlugin(/node:/, (resource) => {
  const mod = resource.request.replace(/^node:/, "");
  switch (mod) {
    case "buffer":
      resource.request = "buffer";
      break;
    case "stream":
      resource.request = "readable-stream";
      break;
    default:
      throw new Error(`Not found ${mod}`);
  }
}),

But I've got another problem in strtok3 package as it uses Buffer without import. For example when file-type use tokenizer.readToken it results in an error.

@Borewit
Copy link
Collaborator

Borewit commented Dec 10, 2021

Thanks for sharing your solution @AaronPorts, that looks like a very reasonable solution to me.

I have released strtok3@v7.0.0-alpha.5, which got explicit Buffer imports.

If you reinstall file-type dependency, is the strtok3 import issue resolved?

@AaronPorts
Copy link
Author

@Borewit I tried alpha.5 version but have module has no exports errors. Probably this is because of CommonJS output instead of ES2020 in TypeScript.

@Borewit
Copy link
Collaborator

Borewit commented Dec 11, 2021

@Borewit I tried alpha.5 version but have module has no exports errors. Probably this is because of CommonJS output instead of ES2020 in TypeScript.

You are right, forgot to compile code before publishing, another attempt: 7.0.0-alpha.6

@AaronPorts
Copy link
Author

I've just checked, strtok3 7.0.0-alpha.6 seems good, but the same issue with Buffer import in token-types 4.1.1

@Borewit
Copy link
Collaborator

Borewit commented Dec 13, 2021

I've just checked, strtok3 7.0.0-alpha.6 seems good, but the same issue with Buffer import in token-types 4.1.1

Sorry for my late reply. token-types is no ES module yet, and id does not like if I create an explicit import to 'node:Buffer'.
I did create myself a little task to make it en ES-module: Borewit/token-types#389

Borewit added a commit that referenced this issue Dec 14, 2021
Update "strtok3" to "^7.0.0-alpha.5".
Update "token-types" to "^5.0.0-alpha.0" (first ES module).

Resolves: #502
Borewit added a commit that referenced this issue Dec 14, 2021
Update "strtok3" to "^7.0.0-alpha.5".
Update "token-types" to "^5.0.0-alpha.0" (first ES module).

Resolves: #502
@Borewit
Copy link
Collaborator

Borewit commented Dec 17, 2021

@AaronPorts can you let us know if your issue is now resolved?

@Borewit Borewit reopened this Dec 17, 2021
@AaronPorts
Copy link
Author

@Borewit It seems that token-types 5.0.0-alpha.1 has wrong exports Borewit/token-types#393 (comment)

@Borewit
Copy link
Collaborator

Borewit commented Dec 17, 2021

@Borewit It seems that token-types 5.0.0-alpha.1 has wrong exports Borewit/token-types#393 (comment)

Missed that one, created Borewit/token-types#396, PR Borewit/token-types#397, fixed in v5.0.0-alpha.2.

@Borewit
Copy link
Collaborator

Borewit commented Dec 20, 2021

Can this one be closed now @AaronPorts ?

@AaronPorts
Copy link
Author

Sorry for the delay, seems good for me.

@Majnuel
Copy link

Majnuel commented Feb 22, 2022

i installed axios as a dependency and this started happening, no idea how to solve it

@lawloretienne
Copy link

Is there a solution for this issue. Seems like it's still broken.

i tried one of the code snippets from the readme.

The stream method can also be used to read from a remote location:

import got from 'got';
import {fileTypeFromStream} from 'file-type';

const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';

const stream = got.stream(url);

console.log(await fileTypeFromStream(stream));
//=> {ext: 'jpg', mime: 'image/jpeg'}

but looks like its an issue with the imports.

Is there a fix for this?

@Alex37882388
Copy link

Alex37882388 commented Oct 16, 2022

I modified it to support browsers based on this repo, I hope it can help you:
https://www.npmjs.com/package/file-type-browser

@EurekaChen
Copy link

I modified it to support browsers based on this repo, I hope it can help you: https://www.npmjs.com/package/file-type-browser

but how to use? can you give a example?

@italoiz
Copy link

italoiz commented Nov 26, 2023

I faced this issue when building my Next.JS application, which uses version 14.0.0. And I put 'use server'; at the top level of the file, and I could build the application.

Example

'use server'; // <<< --- this does the trick

// ... rest of the file

mnvr added a commit to ente-io/ente that referenced this issue Apr 24, 2024
Current version throws
    Module build failed: UnhandledSchemeError: Reading from "node:buffer" is not handled by plugins (Unhandled scheme).
    Webpack supports "data:" and "file:" URIs by default.
    You may need an additional plugin to handle "node:" URIs.

See: sindresorhus/file-type#502

There are ways around by using polyfills, e.g.
- https://stackoverflow.com/questions/76500464/issues-while-using-gradio-client-in-next-js/76522223#76522223
- vercel/next.js#33982

but since no functional changes in the detection that impact us have been made,
stay back at the old version for now.
@GustavoOS
Copy link

I have the same problem when using a Next (14.2.3) component with dynamic import

@alextorn
Copy link

alextorn commented Jun 26, 2024

Still doesn't work in browsers, when using inside nextjs client components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.