Borewit / music-metadata-browser

Browser version of music-metadata parser Supporting a wide range of audio and tag formats.
MIT License
240 stars 21 forks source link

Uncaught ReferenceError: `Buffer` is not defined #417

Closed olyop closed 4 months ago

olyop commented 3 years ago

I am receiving this error: Capture

I am importing the package like this: import { parseBlob } from "music-metadata-browser"

I am calling the function like this: const metadata = await parseBlob(audio)

I have already checked that I am passing a object of type File using instanceof.

I am using the latest version of music-metadata and Chrome.

Borewit commented 3 years ago

I think you are missing buffer dependency inclusion somewhere in your bundler.

olyop commented 3 years ago

Ok, I am using webpack so how do I make it include buffer as a dependency?

Borewit commented 3 years ago

Maybe this article can help you: How to Polyfill node core modules in webpack 5.

olyop commented 3 years ago

Thank you, I managed to find a solution. For anyone else looking, It's an issue with webpack 5.

n1ru4l commented 3 years ago

For anyone using a modern bundler like vite or snowpack, you will have to add yarn add buffer and create a polyfill file where you assign it the window object.

import { Buffer } from "buffer";

if (typeof window !== "undefined" && typeof Buffer === "undefined") {
  window.Buffer = Buffer;
}

I was wondering whether this library could instead operate on Uint8Arrays instead of buffers?

Borewit commented 3 years ago

I was wondering whether this library could instead operate on Uint8Arrays instead of buffers?

That's a good question, I have been been working on that behind the screens. One of the issues is that the TextEncoder is limited to utf-8 encoding and decoding in the Node.js implementation.

Related to:

vaishnavsm commented 3 years ago

For anyone using a modern bundler like vite or snowpack, you will have to add yarn add buffer and create a polyfill file where you assign it the window object.

import { Buffer } from "buffer";

if (typeof window !== "undefined" && typeof Buffer === "undefined") {
  window.Buffer = Buffer;
}

I think you meant typeof window.Buffer === "undefined"

Also, if you are using rollup, I've found that you have to install events yourself. The plugins for rollup don't work right now, and they don't seem to be necessary.

Thanks for the great work! :)

serious-angel commented 5 months ago

Related: https://github.com/vitejs/vite/discussions/2785 (Using (Node’s) Buffer in your polymorphic Vite apps...)

Borewit commented 4 months ago

Maybe we can get rid of Buffer Borewit/token-types#650, keeping this one open to represent the aim to get rid of Buffer. But it maybe that music-metadata replaces music-metadata-browser taking over as good enough browser compliant module.

Borewit commented 4 months ago

music-metadata now offers Node.js free functionality for non Node.js engines.

Polyfilling Buffer or Node.js streams should belong to the past, and it also makes this package obsolete.

But... only available as ECMA Script Module (ESM), CommonJS is no longer supported.

olyop commented 4 months ago

Thank you for your work @Borewit

Just tested it out in a project and works great, no Buffer dependency needed

Borewit commented 4 months ago

Thank you for your work @Borewit

Just tested it out in a project and works great, no Buffer dependency needed

I was a complex and a time consuming architectural change, so glad to hear it worked out for you, thanks for providing that feedback.