I deploy next js on cloudflare pages and get an error like this : Error: Could not access built-in Node.js modules. Please make sure that your Cloudflare Pages project has the 'nodejs_compat' compatibility flag set.
I tried to search the forum and didn't find an answer
Just happened with my Next.js project. After successfuly building and deploying I saw this error on my project URL: Error: Could not access built-in Node.js modules. Please make sure that your Cloudflare Pages project has the 'nodejs_compat' compatibility flag set.
Added nodejs_compat
flag and retried deployment and voila, it works now.
However, the documentation is a bit unclear about how to enter "nodejs_compat" flag from Cloudflare dashboard -> Workers & Pages -> Your Pages project -> Settings -> Functions -> Compatibility Flags. It only gives example regarding putting it inside wrangler.toml as this format:
compatibility_flags = [ "nodejs_compat" ]
If you want to enter "nodejs_compat" flag from Cloudflare dashboard, you need to enter this flag with bare words: nodejs_compat
. Nothing else, no quotation marks, no other words (no brackets or anything else) and it will work.
Important Note: After setting the Compatibility Flags, you need to re-deploy your application in order for them to take effect.