Can Workers Function use npm packages that use node apis?

I want to use the npm package thirty-two (https://www.npmjs.com/package/thirty-two). It was last published 7 years ago seems to use the Buffer api in node. In wrangler.toml, setting compatibility_flags = [ "nodejs_compat" ] was not enough. I also had to mutate globalThis. Is there a cleaner way to do this? What is the recommended approach to using npm packages that use node apis without an import with the node specifier (eg. import { Buffer } from 'node:buffer)
import * as base32 from 'thirty-two';
import { Buffer } from 'node:buffer';

export default {
async fetch(request: Request, env: {}, ctx: ExecutionContext): Promise<Response> {
globalThis.Buffer = Buffer;
const data = {
encode: base32.encode('node').toString(),
decode: base32.decode('NZXWIZI=').toString(),
};
return new Response(JSON.stringify(data, null, 2));
},
};
import * as base32 from 'thirty-two';
import { Buffer } from 'node:buffer';

export default {
async fetch(request: Request, env: {}, ctx: ExecutionContext): Promise<Response> {
globalThis.Buffer = Buffer;
const data = {
encode: base32.encode('node').toString(),
decode: base32.decode('NZXWIZI=').toString(),
};
return new Response(JSON.stringify(data, null, 2));
},
};
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server