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
Issue with nodejs_compat + dynamic require of node:events #854
Comments
Same exact issue for me as well using same package |
Having the same issue with trying to use
This was with Wrangler version v.2.20.0 as well as v3.0.0. Also encountering this error with |
Anyone have any luck resolving this? |
Right now, the only way to access these compatible libraries is by a static |
I'm using |
Which workers syntax are y'all using? The older "server worker" syntax (using |
@jasnell here's useless but functional barebones example where I see the problem: worker.ts
wrangler.toml
package.json
I haven't deployed this to the actual worker instance, but I see the issue I was seeing before with miniflare locally:
|
@dcrodman — looks like this issue is specific to the legacy service worker syntax. I can reproduce it, but only when using the addEventListener('fetch', (event) => {
let fetchEvent: FetchEvent = event as FetchEvent;
fetchEvent.respondWith(handle(fetchEvent));
}); Instead, please use the ESM syntax: export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
let b = Buffer.from(request.body);
console.log(b);
return new Response('Hello World!');
},
}; I'll make sure to update our docs to be clearer about this, and we'll take a look at our error messages in this case. For more on ESM syntax, and to migrate from using service workers, refer to: https://developers.cloudflare.com/workers/learning/migrate-to-module-workers/ |
Thank you @irvinebroque , the docs update feels like the most important part here since this can create a potentially difficult situation for people trying to upgrade to Wrangler 3. Migrating is still not necessarily easy and knowing this up front would've been super helpful. |
I think my statement above was misleading and not quite right. Simple reproduction of the issue: https://github.com/irvinebroque/nodejs_compat_issue/blob/main/src/worker.ts // This does not work
const { Writable } = require('node:stream');
// This works
// import { Writable } from "node:stream";
const myStream = new Writable();
export default {
async fetch(request) {
return new Response("hello");
},
}; We will take a look at what we can do here. |
|
The issue still persists in the latest |
@irvinebroque ... the |
Ah right, unless the userland code is uploaded as a Node.js module type (#564, only supported in workerd, not by surrounding tooling) or CommonJS module type (only used if manually configured AFAIK) — then you can't use But that because Wrangler runs esbuild, it effectively papers over this — it looks like Scaffolding out some better docs in cloudflare/cloudflare-docs#13344 |
I'm going to remove the |
Which Cloudflare product(s) does this pertain to?
Workers/Other, Wrangler
What version of
Wrangler
are you using?2.18.0
What operating system are you using?
Mac
Describe the Bug
Attempting to upload a worker with Wrangler using
compatibility_flags = ['nodejs_compat']
or command line--compatibility-flags="nodejs_compat"
fails with this error message:The package using
node:events
is this LaunchDarkly SDK: https://github.com/launchdarkly/js-core/tree/main/packages/sdk/cloudflareThe text was updated successfully, but these errors were encountered: