Error: Could not access built-in Node.js modules

1.3k Views Asked by At

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

2

There are 2 best solutions below

1
Elmar On BEST 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.

2
Weetz On

You can find the documentation for compatibility flags at https://developers.cloudflare.com/workers/configuration/compatibility-dates/#compatibility-flags

To enable Node.js APIs you'll have to add

compatibility_flags = [ "nodejs_compat" ]

to your wrangler.toml file.

Alternatively you can set compatibility flags in your Cloudflare dashboard by following these steps:

  • Log into the Cloudflare dashboard and select your account.
  • Select Workers & Pages and select your Pages project.
  • Select Settings > Functions > Compatibility Flags.
  • Configure your Production and Preview compatibility flags as needed.