node-js-server-sdk icon indicating copy to clipboard operation
node-js-server-sdk copied to clipboard

Impossible to use cloudflare worker nodejs-compatibility mode due to transitive imports

Open dsumac opened this issue 2 years ago • 4 comments

Context

Bug

  • to use native api, we have to activate compatibility mode without add polyfill
  • when we activate this option the worker expects an import like node:...
  • there is transitively an import without prefix

image

  • so we can not use your lib with native prefix import

Analysis

You use sha.js which use safe-buffer which uses native node Buffer api without node prefix import node:buffer

image

dsumac avatar Nov 22 '23 10:11 dsumac

Hey @dsumac, thanks for sharing these details. We are aware of this incompatibility and plan to replace the sha.js dependency. Until then are you able to workaround this by using polyfills instead of compatibility mode?

kenny-statsig avatar Nov 22 '23 17:11 kenny-statsig

Thanks @kenny-statsig. We use node_compat = true(in our wrangler.toml config file) option to use polyfill. But it doesn't work. Error at deployement.

dsumac avatar Nov 23 '23 11:11 dsumac

@dsumac Can you try adding compatibility_date. This works fine for me:

compatibility_date = "2023-01-20"
node_compat = true

cpreid avatar Nov 27 '23 13:11 cpreid

@cpreid , my explicate was maybe not clear.

Effectively this config it works. Even with an older compat mode. And this is this kind of config which works.

But I would like use native nodejs api in the same time, in the same worker code. Ex:

import { createHash } from "node:crypto";

const hash = createHash("valueToHash");

....

To directly use node api like this without polyfill we must activate nodejs compatibility flag: https://developers.cloudflare.com/workers/configuration/compatibility-dates/#nodejs-compatibility-flag

And when we activate this config, it doesn't work:

compatibility_date = "2023-01-20"
compatibility_flags = [ "nodejs_compat" ]

# we must disable node_compat to use compatibility_flags
# node_compat = true

dsumac avatar Dec 06 '23 19:12 dsumac