A build recently failed in Cloudflare Pages and showed the following error.
19:09:34.631 Executing user command: npm run build 19:09:34.980 19:09:34.981 > firstname.lastname@example.org build /opt/buildhome/repo 19:09:34.981 > vite build 19:09:34.981 19:09:35.288 failed to load config from /opt/buildhome/repo/vite.config.js 19:09:35.289 error during build: 19:09:35.289 Error: Cannot find module 'happy-dom'
npm run build on my local machine worked fine so I went on to find out the difference between my machine and the Cloudflare Pages build environment.
It turned out that my machine was on Node 16.15.0 while Cloudflare Pages was at 12.18.0, which is quite old. So, based on the build configuration docs, all we need to do is set an environment variable in the build settings of the Cloudflare Pages project for both production and preview
NODE_VERSION=16.15.0 so it matches the local build environment.