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

TypeError: import_qrcode.default.toBuffer is not a function when running in Cloudflare Workers #376

Open
vladinator1000 opened this issue Sep 20, 2024 · 1 comment

Comments

@vladinator1000
Copy link

vladinator1000 commented Sep 20, 2024

Hi there, it seems like the library doesn't work on Cloudflare even with the compatibility flags in wrangler.toml:
compatibility_flags = ["nodejs_compat_v2"]

Steps to reproduce:

  1. Clone this repo: https://github.com/vladinator1000/cf-qrcode
  2. Run npm start
  3. Go to localhost:8787

I can see that toBuffer is clearly exported from https://github.com/soldair/node-qrcode/blob/master/lib/server.js#L106
I wonder if the issue is due to the bundling process. When I enumerate the package keys like this

import QRCode from 'qrcode';

export default {
	async fetch(request, env, ctx): Promise<Response> {
		return new Response(Object.keys(QRCode));
	},
} satisfies ExportedHandler<Env>;

I get

create,toCanvas,toDataURL,toString
@vladinator1000
Copy link
Author

vladinator1000 commented Sep 20, 2024

I managed to get it to work by importing directly from the server folder

import { toBuffer } from 'qrcode/lib/server'

// Bonus promisification
export async function getQrCodeData(url: string): Promise<Buffer> {
  return new Promise((resolve, reject) => {
    function callback(err: Error, result: Buffer) {
      if (err) {
        reject(err)
      } else {
        resolve(result)
      }
    }

    toBuffer(url, callback)
  })
}

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