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 #13

Open
chocnut opened this issue Dec 11, 2021 · 4 comments
Open

Uncaught ReferenceError: Buffer is not defined #13

chocnut opened this issue Dec 11, 2021 · 4 comments

Comments

@chocnut
Copy link

chocnut commented Dec 11, 2021

When executing wrangler publish I'm getting a Buffer is undefined.

Screenshot 2021-12-11 at 6 55 02 PM

@elsell
Copy link

elsell commented Feb 9, 2022

I have also had this issue. Downgrading ts-loader seemed to do the trick for now:

npm install --save-dev ts-loader@~8.2.0

https://stackoverflow.com/a/68806961/5001439 seems to indicate that upgrading Webpack may also be a solution.

@lem0n4id
Copy link

same issue here. @elsell how did solve this?
I did both npm install -g ts-loader@8.2.0 and npm install webpack@5.40.0. It still gives the same error

cli output 👇 (click to expand)
$ wrangler publish
 Running npm install && npm run build

up to date, audited 611 packages in 3s

52 packages are looking for funding
  run `npm fund` for details

3 moderate severity vulnerabilities

To address all issues, run:
  npm audit fix

Run `npm audit` for details.

> worker-typescript-template@1.0.0 build
> webpack

asset worker.js 90.9 KiB [compared for emit] (name: main) 1 related asset
modules by path ./node_modules/@glenstack/ 21.7 KiB
  modules by path ./node_modules/@glenstack/cf-workers-fetch-helpers/dist/*.js 6.52 KiB 6 modules
  modules by path ./node_modules/@glenstack/cf-workers-discord-bot/dist/*.js 11.8 KiB
    ./node_modules/@glenstack/cf-workers-discord-bot/dist/index.js 1.28 KiB [built] [code generated]
    ./node_modules/@glenstack/cf-workers-discord-bot/dist/authorize.js 1.26 KiB [built] [code generated]
    ./node_modules/@glenstack/cf-workers-discord-bot/dist/interaction.js 2.78 KiB [built] [code generated]
    ./node_modules/@glenstack/cf-workers-discord-bot/dist/types.js 2.64 KiB [built] [code generated]
    ./node_modules/@glenstack/cf-workers-discord-bot/dist/setup.js 3.87 KiB [built] [code generated]
  ./node_modules/@glenstack/cf-workers-router/dist/index.js 3.33 KiB [built] [code generated]
./src/index.ts 1.02 KiB [built] [code generated]
./node_modules/tweetnacl/nacl-fast.js 60.5 KiB [built] [code generated]
crypto (ignored) 15 bytes [built] [code generated]
webpack 5.40.0 compiled successfully in 1299 ms
 Build completed successfully!
Error: Something went wrong with the request to Cloudflare...
Uncaught ReferenceError: Buffer is not defined
  at line 357 in btoa
  at line 361 in exports.authorization
  at line 222 in __webpack_modules__../node_modules/@glenstack/cf-workers-discord-bot/dist/setup.js.exports.setup
  at line 68 in __webpack_modules__../node_modules/@glenstack/cf-workers-discord-bot/dist/index.js.exports.createSlashCommandHandler
  at line 3073
  at line 3083
  at line 3085
 [API code: 10021]

@studentIvan
Copy link

studentIvan commented Mar 22, 2022

Hi @chocnut maybe it will help you

import { encode } from '@cfworker/base64url';

if (typeof Buffer === 'undefined' && typeof global !== 'undefined') {
    global.Buffer = {
      // @ts-ignore
      isBuffer: s => typeof s === 'string',
      // @ts-ignore
      from: s => ({ toString: (m) => m === 'base64' ? encode(s).toString() : (m || '') }),
      // @ts-ignore
      byteLength: s => s.length,
    };
  }

@Code6226
Copy link

Code6226 commented May 1, 2022

I forked this project and applied the fix you suggested. I had to fix a reliance on Buffers in one more place, but now it works!

https://github.com/Code6226/glenstack/tree/master/packages/cf-workers-discord-bot

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

No branches or pull requests

5 participants