workos-node icon indicating copy to clipboard operation
workos-node copied to clipboard

Cloudflare Workers with "nodejs_compat" error: Dynamic require of "buffer/index.js" is not supported

Open josephbuchma opened this issue 6 months ago • 9 comments

"@workos-inc/node": "7.59.0"

Getting an error after enabling "nodejs_compat" compatibility flag in wrangler.jsonc. Full stack trace:

error running node_modules/.vite/deps_app/@workos-inc_node.js?v=54cb8c7e

Error: Dynamic require of "buffer/index.js" is not supported
    at node_modules/.vite/deps_app/chunk-UP2VWCW5.js:9:9
    at ../../node_modules/.pnpm/[email protected]/node_modules/iron-webcrypto/dist/index.js (node_modules/.vite/deps_app/@workos-inc_node.js:1610:29)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]_/node_modules/iron-session/edge/dist/index.js (node_modules/.vite/deps_app/@workos-inc_node.js:1902:24)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]_/node_modules/@workos-inc/node/lib/common/iron-session/edge-iron-session-provider.js (node_modules/.vite/deps_app/@workos-inc_node.js:2148:18)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]_/node_modules/@workos-inc/node/lib/index.worker.js (node_modules/.vite/deps_app/@workos-inc_node.js:12311:40)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at node_modules/.vite/deps_app/@workos-inc_node.js:12362:16

error running worker/hono/api.ts
Error: Dynamic require of "buffer/index.js" is not supported
    at node_modules/.vite/deps_app/chunk-UP2VWCW5.js:9:9
    at ../../node_modules/.pnpm/[email protected]/node_modules/iron-webcrypto/dist/index.js (node_modules/.vite/deps_app/@workos-inc_node.js:1610:29)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]_/node_modules/iron-session/edge/dist/index.js (node_modules/.vite/deps_app/@workos-inc_node.js:1902:24)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]_/node_modules/@workos-inc/node/lib/common/iron-session/edge-iron-session-provider.js (node_modules/.vite/deps_app/@workos-inc_node.js:2148:18)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]_/node_modules/@workos-inc/node/lib/index.worker.js (node_modules/.vite/deps_app/@workos-inc_node.js:12311:40)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at node_modules/.vite/deps_app/@workos-inc_node.js:12362:16
error running worker/index.ts
Error: Dynamic require of "buffer/index.js" is not supported
    at node_modules/.vite/deps_app/chunk-UP2VWCW5.js:9:9
    at ../../node_modules/.pnpm/[email protected]/node_modules/iron-webcrypto/dist/index.js (node_modules/.vite/deps_app/@workos-inc_node.js:1610:29)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]_/node_modules/iron-session/edge/dist/index.js (node_modules/.vite/deps_app/@workos-inc_node.js:1902:24)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]_/node_modules/@workos-inc/node/lib/common/iron-session/edge-iron-session-provider.js (node_modules/.vite/deps_app/@workos-inc_node.js:2148:18)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at ../../node_modules/.pnpm/@[email protected][email protected][email protected][email protected][email protected]_/node_modules/@workos-inc/node/lib/index.worker.js (node_modules/.vite/deps_app/@workos-inc_node.js:12311:40)
    at __require2 (node_modules/.vite/deps_app/chunk-UP2VWCW5.js:15:50)
    at node_modules/.vite/deps_app/@workos-inc_node.js:12362:16

7:23:52 PM [vite] Internal server error: Dynamic require of "buffer/index.js" is not supported
      at async ProxyServer.fetch (node_modules/miniflare/src/workers/core/proxy.worker.ts:173:11)

josephbuchma avatar Jul 10 '25 17:07 josephbuchma

Important note: it is an issue when running locally on miniflare only. Here is my current workaround for local development:

export const createWorkOSClient = async (env: Env): Promise<IWorkOS> => {
	if (env.MOCK_WORKOS === 'true') {
		return new WorkOSLocalDevMock()
	}
        
	const { WorkOS } = await import('@workos-inc/node')
	return new WorkOS(env.WORKOS_API_KEY, {
		clientId: env.WORKOS_CLIENT_ID,
	})
}

josephbuchma avatar Jul 13 '25 19:07 josephbuchma

@josephbuchma was this working in an earlier version of @workos-inc/node?

TannerPerrien avatar Aug 02 '25 18:08 TannerPerrien

I am getting the same thing and happens specifically if I am using cloudflare/vite-plugin.

ivogt avatar Aug 02 '25 19:08 ivogt

@TannerPerrien I think I've tried to downgrade the version, but not sure. Fortunately I found the workaround relatively quickly, because I don't have bandwidth for debugging this.

josephbuchma avatar Aug 07 '25 17:08 josephbuchma

+1 experiencing this issue.

Any chance we can see your WorkOSLocalDevMock() implementation? @josephbuchma

samcxps avatar Aug 25 '25 23:08 samcxps

Sorry for not adding more details about the mock. I abandoned that idea because mocking that class turned out to be very cumbersome. Instead I just lifted the localhost auth logic to the HTTP handlers.

const devAuthDefault = {
	authenticated: false,
	userId: 2,
	user: {
		object: 'user',
		id: 'user_XXXXXXXXXXXXXXXXXXXXXXXXXX',
		email: '[email protected]',
		emailVerified: true,
		firstName: 'Yosyp',
		profilePictureUrl:
			'https://workoscdn.com/images/v1/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-Ho',
		lastName: 'Buchma',
		lastSignInAt: '2025-07-13T09:57:12.188Z',
		createdAt: '2025-07-12T12:26:34.635Z',
		updatedAt: '2025-07-13T09:57:12.188Z',
		externalId: '2',
		metadata: {},
	},
}
let devAuth: typeof devAuthDefault | null = null

export const getDevAuth = (requestUrl: string) => {
	if (requestUrl.startsWith('http://localhost') && !devAuth) {
		devAuth = devAuthDefault
	}
	return devAuth
}

// Auth TRPC router
export const authAPIRouter = t.router({
	// Handle auth callback
	handleCallback: t.procedure
		.input(z.object({ code: z.string() }))
		.mutation(async ({ input, ctx }): Promise<{ user: User; ok: boolean }> => {
			if (!input.code) {
				throw new TRPCError({ code: 'BAD_REQUEST', message: 'No code provided' })
			}
                         
            // LOCALHOST AUTH WORKAROUND:
			const devAuth = getDevAuth(ctx.req.url)
			if (devAuth) {
				devAuth.authenticated = true
				return {
					ok: true,
					user: devAuth.user as User,
				}
			}

            // ^^^^^^^^^^^^^^^^^^^ I added similar block for all auth-related endpoints (and in auth middleware as well)


josephbuchma avatar Aug 26 '25 09:08 josephbuchma

Hi all, since this issue appears to be more to the Cloudflare vite plugin than to @workos-inc/node, I have opened an issue there as well.

Feel free to comment/react for visibility 😄

Issue here

samcxps avatar Aug 27 '25 00:08 samcxps

@josephbuchma @ivogt

I downgraded the specific dependency causing the issue, iron-webcrypto, and it appears to be working fine with the vite plugin.

In my package.json

...
"overrides": {
    "iron-webcrypto": "1.2.0"
},
...

samcxps avatar Sep 04 '25 14:09 samcxps

@josephbuchma @ivogt

I downgraded the specific dependency causing the issue, iron-webcrypto, and it appears to be working fine with the vite plugin.

In my package.json

...
"overrides": {
    "iron-webcrypto": "1.2.0"
},
...

Thank you. This has been huge pain. I had to move to rest api calls to work around that.

ivogt avatar Sep 04 '25 14:09 ivogt