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

Uncaught ReferenceError: Buffer is not defined #417

Closed
olyop opened this issue Jan 9, 2021 · 12 comments
Closed

Uncaught ReferenceError: Buffer is not defined #417

olyop opened this issue Jan 9, 2021 · 12 comments
Labels
question Further information is requested

Comments

@olyop
Copy link

olyop commented Jan 9, 2021

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
Copy link
Owner

Borewit commented Jan 9, 2021

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

@olyop
Copy link
Author

olyop commented Jan 9, 2021

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

@Borewit
Copy link
Owner

Borewit commented Jan 10, 2021

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

@Borewit Borewit added the question Further information is requested label Jan 10, 2021
@olyop
Copy link
Author

olyop commented Jan 11, 2021

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

@olyop olyop closed this as completed Jan 11, 2021
@n1ru4l
Copy link

n1ru4l commented Mar 12, 2021

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
Copy link
Owner

Borewit commented Mar 13, 2021

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
Copy link

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! :)

@artshade
Copy link

Related: vitejs/vite#2785 (Using (Node’s) Buffer in your polymorphic Vite apps...)

@Borewit
Copy link
Owner

Borewit commented Jun 28, 2024

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
Copy link
Owner

Borewit commented Jul 10, 2024

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.

@Borewit Borewit closed this as completed Jul 10, 2024
@olyop
Copy link
Author

olyop commented Jul 11, 2024

Thank you for your work @Borewit

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

@Borewit
Copy link
Owner

Borewit commented Jul 11, 2024

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.

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

No branches or pull requests

5 participants