file-type: node: prefix error using in browser

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.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 19

Commits related to this issue

Most upvoted comments

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 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.

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

Sorry for the delay, seems good for me.