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

ReferenceError: Buffer is not defined #487

Closed
fabriguespe opened this issue Nov 2, 2023 · 1 comment
Closed

ReferenceError: Buffer is not defined #487

fabriguespe opened this issue Nov 2, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@fabriguespe
Copy link
Contributor

fabriguespe commented Nov 2, 2023

Describe the bug

Hello,

I encountered an issue while using the @xmtp/xmtp-js package in a React application running in the browser. The error message is ReferenceError: Buffer is not defined.

It seems that the package is trying to use Node.js's Buffer object, which is not available in the browser environment. This causes the application to crash.

CleanShot 2023-11-02 at 16 50 36@2x

Here's the error stack trace for reference:

Buffer is not defined
ReferenceError: Buffer is not defined
    at ./node_modules/@xmtp/xmtp-js/dist/web/index.js (http://localhost:3000/static/js/bundle.js:94369:8)
    at options.factory (http://localhost:3000/static/js/bundle.js:108687:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:108097:33)
    at fn (http://localhost:3000/static/js/bundle.js:108344:21)
    at ./src/FloatingInbox/index.js (http://localhost:3000/static/js/bundle.js:1355:71)
    at options.factory (http://localhost:3000/static/js/bundle.js:108687:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:108097:33)
    at fn (http://localhost:3000/static/js/bundle.js:108344:21)
    at ./src/Page.js (http://localhost:3000/static/js/bundle.js:1765:72)
    at options.factory (http://localhost:3000/static/js/bundle.js:108687:31)
@fabriguespe fabriguespe added the bug Something isn't working label Nov 2, 2023
@fabriguespe
Copy link
Contributor Author

If you get into issues with Buffer and polyfills check out the fix below:

  1. Install the buffer dependency.
npm i buffer
  1. Create a new file, polyfills.js, in the root of your project.
import { Buffer } from "buffer";

window.Buffer = window.Buffer ?? Buffer;
  1. Import it into your main file on the first line.
  • ReacJS: index.js or index.tsx
  • VueJS: main.js
  • NuxtJS: app.vue
//has to be on the first line of the file for it to work
import "./polyfills";
  1. Update config files.
  • Webpack: vue.config.js or webpack.config.js:
const webpack = require("webpack");

module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        Buffer: ["buffer", "Buffer"],
      }),
    ],
  },
  transpileDependencies: true,
};
  • Vite: vite.config.js:
import { defineConfig } from "vite";
import { Buffer } from "buffer";

export default defineConfig({
  /**/
  define: {
    global: {
      Buffer: Buffer,
    },
  },
  /**/
});
  • NuxtJS: nuxt.config.js:
export default {
  build: {
    extend(config, { isClient }) {
      if (isClient) {
        config.node = {
          Buffer: true,
        };
      }
    },
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant