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.

Buffer is not defined

See original GitHub issue

What version of Remix are you using?

1.3.5

Steps to Reproduce

  • setup a remix cloudflare-worker project with mantine or any other library that uses Buffer. I followed the remix directions on the mantine site.
  • remix build: works fine
  • wrangler publish : fails

I can make a demo project that fails, if you want to try to deploy it to your own cloudflare.

Expected Behavior

Should output code that can be deployed on cloudflare.

Actual Behavior

It was reported at #2625 but said to be fixed in 1.3.5, so it may be some combination of remix/mantine/etc. I can build in remix fine, but on deploy (via wrangler), I get this:

✘ [ERROR] Received a bad response from the API

  Uncaught ReferenceError: Buffer is not defined
    at line 4699
    at line 33
    at line 5472
    at line 33
    at line 6626
    at line 33
    at line 6704
    at line 33
    at line 94195
   [code: 10021]


  If you think this is a bug, please open an issue at:
  https://github.com/cloudflare/wrangler2/issues/new

Normally, in other builders, a polyfill can be used, either in esbuild config, which we can’t edit in remix, or by importing buffer. If I install buffer module & and this in server entry-point:

/* global addEventListener  */

import buffer from 'buffer'

import { createEventHandler } from '@remix-run/cloudflare-workers'
import * as build from '@remix-run/dev/server-build'

globalThis.Buffer = buffer

addEventListener(
  'fetch',
  createEventHandler({ build, mode: process.env.NODE_ENV })
)

I get this error:

Building Remix app in production mode...
It appears you're using a module that is built in to node, but you installed it as a dependency which could cause problems. Please remove buffer before continuing.
Error
    at Object.onBuildFailure (/Users/konsumer/Documents/work/PROJECT/node_modules/@remix-run/dev/cli/commands.js:150:13)
    at buildEverything (/Users/konsumer/Documents/work/PROJECT/node_modules/@remix-run/dev/compiler.js:280:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.build (/Users/konsumer/Documents/work/PROJECT/node_modules/@remix-run/dev/compiler.js:105:3)
    at async Object.build (/Users/konsumer/Documents/work/PROJECT/node_modules/@remix-run/dev/cli/commands.js:145:3)
    at async Object.run (/Users/konsumer/Documents/work/PROJECT/node_modules/@remix-run/dev/cli/run.js:467:7)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:8

github_iconTop GitHub Comments

3reactions
3x071ccommented, Apr 23, 2022

Cloudflare runs on V8 natively, without the additional Node.js abstraction. This means certain APIs, such as Buffer, aren’t available for your dependencies to use in Cloudflare Workers/Pages. Try this, it allows you to override Remix’s esbuild configuration to inject/alias polyfills. Here’s an example on how to replace the Buffer API. If you want a full production usage example, check out this repository. I agree that Remix should be polyfilling Buffer by default on CF though, it’s definitely a hurdle for adoption. (I think there already is an issue about that)

2reactions
kevinwolfcrcommented, Aug 12, 2022

@konsumer I just followed this guide and it worked.

I am using all the latest versions from remix and mantine packages as of today.

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 #1626 - GitHub
Trying to bundle a project which includes https://solana-labs.github.io/wallet-adapter. First, I had a problem with global not being defined ...
Read more >
Buffer is not defined. Using Phantom Wallet, Solana and ...
This is happening because the default bundler that comes with create react app(webpack 5) does not polyfill Buffer .
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 >
ReferenceError: Buffer is not defined - Homey Community Forum
Buffer is a global variable set by homeyscript. Now it is not defined anymore. Why do you need the script? If you think...
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