简体   繁体   中英

How i can solve Electron error: "Buffer is not defined"?

I am developing an application on emberjs and using electron for the desktop version.

I connected the eosjs-ecc addon and looking at its sources I realized that it uses Buffer, but this error "Buffer is not defined" occurs.

I tried several options among which webPreferences: { nodeIntegration: true } and that didn't help either.

Someone solved a similar problem? Thanks for answers!

Since Buffer isn't available on the web, you need to polyfill it.

And since modern ember uses webpack, polyfilling Buffer is well documented. Here is a list of plugins webpack provides out of the box.

If you're only using ember-auto-import, in your ember-cli-build.js, you'd provide your webpack config like this:

// ember-cli-build.js

// ...

let app = new EmberApp(defaults, {
  // ...
  autoImport: {
    webpack: {
      plugins: [
        new webpack.ProvidePlugin({
          Buffer: ['buffer', 'Buffer'],
        }),
      ],
    }
  }
});

or if you're using embroider, you'd configure webpack this way:

// ember-cli-build.js

const { Webpack } = require('@embroider/webpack');
return require('@embroider/compat').compatBuild(app, Webpack, {
  packagerOptions: {
    webpackConfig: {
     plugins: [
        new webpack.ProvidePlugin({
          Buffer: ['buffer', 'Buffer'],
        }),
      ],
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM