ReferenceError: crypto is not defined

I have a javascript file which makes use of crypto. This file is used by both my Cloudflare Pages Functions and my Node preinstall script when running npm i. Locally, on my PC, both run fine but on Cloudflare Pages the preinstall script crashes with ReferenceError: crypto is not defined. If I add
import * as crypto from 'crypto';
at the top of the file, the preinstall step will run fine but the Pages Function will fail to build with
The package "crypto" wasn't found on the file system but is built into node. Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.
I do have the nodejs_compat flag enabled. Is there any way to not have to use
import * as crypto from 'crypto';
since this isn't required when running the preinstall script on my PC?
Was this page helpful?