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

Buffer error with Cloudflare Workers #32

Open
mistial-dev opened this issue Apr 29, 2024 · 0 comments
Open

Buffer error with Cloudflare Workers #32

mistial-dev opened this issue Apr 29, 2024 · 0 comments

Comments

@mistial-dev
Copy link

  • Version: 8.0.2
  • Platform: CloudFlare Workers

Attempting to use this with Cloudflare Workers, I'm getting an error that buffer isn't defined. I suspect it's because Buffer isn't imported explicitly.

(base) mistial@Mistials-MacBook-Pro xxx % npx wrangler deploy                                            
 ⛅️ wrangler 3.52.0
-------------------
Your worker has access to the following bindings:
- Queues:
  - QUEUE: xxx
- D1 Databases:
  - DB: xxx (xxx-xxx-xxx-xxx-xxx)
Total Upload: xxx KiB / gzip: xxx KiB

✘ [ERROR] A request to the Cloudflare API (/accounts/xxx/workers/scripts/xxx) failed.

  Uncaught ReferenceError: Buffer is not defined
    at null.<anonymous>
  (file:///Users/mistial/Projects/xxx/node_modules/asn1-ts/source/asn1.ts:88:9) in toBytes
    at null.<anonymous> (file:///Users/mistial/Projects/xxx/src/router.ts:12:26)
   [code: 10021]

I am using nodejs compatibility mode:

compatibility_flags = [ "nodejs_compat" ]

Looking through the CloudFlare Documentation, it states:

To ensure compatibility with a wider set of npm packages, and make it easier for you to run existing 
applications on Cloudflare Workers, the following APIs from the Node.js runtime are available directly 
as Workers runtime APIs, with no need to add polyfills to your own code

They show the need to add an import like import { Buffer } from 'node:buffer'; This seems to align with nodejs recommendations:

While the Buffer class is available within the global scope, it is still recommended to explicitly reference
it via an import or require statement.
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

1 participant