question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: Buffer is not defined

See original GitHub issue

On page load I’m getting the error Buffer is not defined:

image

It seems to only happen when I’ve imported the '@metamask/eth-sig-util' script and try to encrypt data:

import ethUtil from 'ethereumjs-util'
import sigUtil from '@metamask/eth-sig-util'

const encryptedData = ethUtil.bufferToHex(
  Buffer.from(
    JSON.stringify(
      sigUtil.encrypt({
        publicKey: publicKey,
        data: 'hello world data',
        version: 'x25519-xsalsa20-poly1305',
      })
    ),
    'utf8'
  )
)

I’ve tried adding in import { Buffer } from 'buffer/', but this doesn’t seem to help. Chrome version: Version 100.0.4896.88 (Official Build) (64-bit) on Ubuntu 20 and in FireFox 100.0 (64-bit).

If I omit import sigUtil from '@metamask/eth-sig-util' or

const encryptedData = ethUtil.bufferToHex(
  Buffer.from(
    JSON.stringify(
      sigUtil.encrypt({
        publicKey: publicKey,
        data: 'hello world data',
        version: 'x25519-xsalsa20-poly1305',
      })
    ),
    'utf8'
  )
)

The error goes away.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
mcmirecommented, Jun 21, 2022

Hi @SyedImam1998. Did you see my instructions above?

1reaction
mcmirecommented, May 13, 2022

Ah, perfect, that makes sense too! Glad I could help. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught ReferenceError: Buffer is not defined - Stack Overflow
Answering my own question. Two things helped to resolve the issue: Adding plugins section with ProviderPlugin into webpack.config.js.
Read more >
Uncaught ReferenceError: Buffer is not defined #2248 - GitHub
If you install polyfill buffer package, you get the error: It appears you're using a module that is built in to node, but...
Read more >
Buffer is not defined. Using Phantom Wallet, Solana and ...
testing - Uncaught ReferenceError: Buffer is not defined. Using Phantom Wallet, Solana and React to make a transaction - Solana Stack Exchange. ...
Read more >
ReferenceError: Buffer is not defined - Homey Community Forum
I am running Homey 7.4.1 and HomeyScript v3.3.0. Restarted HomeyScript and also have restarted Homey. No change. No other installs etc done.
Read more >
How to polyfill Buffer with Webpack 5 - viglucci.io
The "buffer is not defined" error is a common error that can occur when trying to use the Buffer Node.js API in an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found