Open cayter opened 2 months ago
Hi there, can you please provide some reproduction steps? I haven't been able to trigger this error in my CFW example app.
buffer
Module Path Error When Using Cloudflare Workers with Node Compatibility v2I'm encountering an issue when using the @workos-inc/node
package in a project running on Cloudflare Workers with nodejs_compat_v2
. The problem occurs due to a path-related error originating from the buffer
module when trying to deploy or run the worker in a development environment.
The error specifically arises when the compatibility_date
in wrangler.toml
is set to September 23rd, 2024, or later, which activates nodejs_compat_v2
. This issue doesn't occur with the older nodejs_compat
version, but I cannot use the older compatibility mode due to other dependencies in my actual project that require the latest version.
This is a simple Cloudflare Worker that utilizes the @workos-inc/node
library to retrieve a list of organizations via an API call. The Worker is set up using Hono.js
, and the environment variables are injected using Cloudflare's bindings.
Below is the code that reproduces the error:
package.json
{
"name": "workos-error",
"scripts": {
"dev": "wrangler dev src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts"
},
"dependencies": {
"@workos-inc/node": "^7.27.3",
"hono": "^4.6.3"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240529.0",
"wrangler": "^3.57.2"
}
}
src/index.ts
import { WorkOS } from "@workos-inc/node";
import { Hono } from "hono";
type Bindings = {
WORKOS_CLIENT_ID: string;
WORKOS_API_KEY: string;
};
const app = new Hono<{ Bindings: Bindings }>();
app.get("/", async (c) => {
const workos = new WorkOS(c.env.WORKOS_API_KEY, {
clientId: c.env.WORKOS_CLIENT_ID,
});
const organizations = await workos.organizations.listOrganizations();
return c.json(organizations);
});
export default app;
wrangler.toml
With the new nodejs_compat_v2
compatibility flag:
name = "workos-error"
compatibility_date = "2024-09-27"
compatibility_flags = ["nodejs_compat_v2"]
When using the older nodejs_compat
compatibility flag, the issue does not occur:
name = "workos-error"
compatibility_date = "2024-09-22"
compatibility_flags = ["nodejs_compat"]
⛅️ wrangler 3.78.11
--------------------
[wrangler:inf] Ready on http://localhost:8787
✘ [ERROR] Plugin "unenv-cloudflare" returned a non-absolute path: buffer (set a namespace if this is not a file path)
node_modules/.pnpm/iron-webcrypto@0.2.8/node_modules/iron-webcrypto/dist/index.js:3:23:
3 │ var index_js = require('buffer/index.js');
╵ ~~~~~~~~~~~~~~~~~
✘ [ERROR] Failed to build
The error originates from a require statement inside the iron-webcrypto
package, which is a dependency of @workos-inc/node
. It seems that require('buffer/index.js')
is causing issues in the Cloudflare Workers environment when using nodejs_compat_v2
. The compatibility update requires paths to be prefixed with node:
when referencing native Node.js modules like buffer
.
In the file node_modules/.pnpm/iron-webcrypto@0.2.8/node_modules/iron-webcrypto/dist/index.js
, the problematic line is:
var index_js = require('buffer/index.js');
This should be changed to:
var index_js = require('node:buffer');
Create a new project:
pnpm init
pnpm add @workos-inc/node hono
pnpm add -D @cloudflare/workers-types wrangler
Create a Cloudflare Worker with the provided index.ts
file.
Update wrangler.toml
with the following content:
name = "workos-error"
compatibility_date = "2024-09-27"
compatibility_flags = ["nodejs_compat_v2"]
Run the Worker in development mode:
pnpm run dev
Observe the Error: The build will fail with the error message mentioned above.
The Worker should start without errors and retrieve a list of organizations from the WorkOS API.
Update the iron-webcrypto
dependency to use require('node:buffer')
instead of require('buffer/index.js')
to align with Node.js module resolution in Cloudflare Workers’ latest compatibility mode (nodejs_compat_v2
).
nodejs_compat_v2
compatibility flag, introduced on September 23rd, 2024, or later.nodejs_compat
flag (before the compatibility update) resolves the issue, but this is not feasible for projects that rely on newer features in nodejs_compat_v2
.@PaulAsjes
Thank you @EnriqueRuvalcaba for the super detailed report! I'll have a look through this and get back to you.
same issue "@workos-inc/node": "^7.10.0"
is the last version working for me.
Hi! Do you have any updates on the issue? I am experiencing the same problem with "@workos-inc/node": "^7.30.0"
on Cloudflare Pages in my SvelteKit app, regardless of whether I use node_compat
or node_compat_v2
.
+1 to this whole thread, I workos is a great tool, but this is limiting for folks building on cloudflare.
In our case we could not implement some observability and distributed tracing from baselime as it requires enabling cloudflare's compatibility_flags
After checking iron-session does seem "just" (famous last words) a package update could be all that's needed, so i tried it and seems to be working on my end.
There's still some issues, made a PR here https://github.com/workos/workos-node/pull/1156 explaining what I found, (feel free to do whatever with that PR).
I did package/release my hacked-together fork in https://www.npmjs.com/package/@fforres/workos-inc-node.
I STRONGLY recommend that you don't use it, here be dragons and all that. It worked on my case, but if you are truly blocked, be my guest npm i @fforres/workos-inc-node
(It just packages what's on that PR)
Cloudflare Workers will now support better NodeJS compatibility as announced here. However, the package currently relies on
iron-webcrypto
that usesbuffer
polyfill and it leads tonodejs_compat_v2
facing the issue below:Current workaround
Replace
buffer/index.js
withnode:buffer
in thenode_modules
deps before the build step.