Error during build on NextJS app in Cloudflare Pages with 'fs' ad 'path' module not found

I’m trying to deploy my NextJS app to Cloudflare Pages but I’m getting errors when building the app.
I followed the instructions given by Cloudflare on how to deploy a NextJS app to Pages (installed the @cloudflare/next-on-pages, added an edge runtime, and used the given build command).

Here’s the log:

2023-11-04T15:12:29.171161Z ▲  ▲ Next.js 14.0.1
2023-11-04T15:12:29.172068Z ▲  Creating an optimized production build ...
2023-11-04T15:12:29.191319Z ▲  ⚠ Custom webpack configuration is detected. When using a custom webpack configuration, the Webpack build worker is disabled by default. To force enable it, set the "experimental.webpackBuildWorker" option to "true". Read more: https://nextjs.org/docs/messages/webpack-build-worker-opt-out
2023-11-04T15:12:29.230929Z ▲  ⚠ You are using an experimental edge runtime, the API might change.
2023-11-04T15:12:29.657053Z ▲  > [PWA] Compile server
2023-11-04T15:12:29.657799Z ▲  > [PWA] Compile server
2023-11-04T15:12:29.658519Z ▲  > [PWA] Compile client (static)
2023-11-04T15:12:29.658933Z ▲  > [PWA] Auto register service worker with: /opt/buildhome/repo/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next-pwa/register.js
2023-11-04T15:12:29.659158Z ▲  > [PWA] Service worker: /opt/buildhome/repo/public/sw.js
2023-11-04T15:12:29.659361Z ▲  > [PWA]   url: /sw.js
2023-11-04T15:12:29.659549Z ▲  > [PWA]   scope: /
2023-11-04T15:12:29.678539Z ▲  > [PWA] Fallback to precache routes when fetch failed from cache or network:
2023-11-04T15:12:29.679026Z ▲  > [PWA]   document (page): /_offline
2023-11-04T15:13:12.745727Z ▲  Failed to compile.
2023-11-04T15:13:12.746602Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:20:0
2023-11-04T15:13:12.747068Z ▲  Module not found: Can't resolve 'fs'
2023-11-04T15:13:12.747574Z ▲  
2023-11-04T15:13:12.747779Z ▲  https://nextjs.org/docs/messages/module-not-found
2023-11-04T15:13:12.748341Z ▲  
2023-11-04T15:13:12.74906Z  ▲  Import trace for requested module:
2023-11-04T15:13:12.749279Z ▲  ./node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js
2023-11-04T15:13:12.74951Z  ▲  ./src/pages/api/auth/[...nextauth].ts
2023-11-04T15:13:12.749772Z ▲  ./src/pages/profile.tsx
2023-11-04T15:13:12.750403Z ▲  ./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-ssr-loader/index.js?{"absolute500Path":"","absoluteAppPath":"private-next-pages/_app.tsx","absoluteDocumentPath":"private-next-pages/_document.tsx","absoluteErrorPath":"next/dist/pages/_error","absolutePagePath":"private-next-pages/profile.tsx","buildId":"rx9hHNN6L2omwvD5EKiyu","dev":false,"isServerComponent":false,"page":"/profile","stringifiedConfig":"eyJlbnYiOnt9LCJlc2xpbnQiOnsiaWdub3JlRHVyaW5nQnVpbGRzIjp0cnVlfSwidHlwZXNjcmlwdCI6eyJpZ25vcmVCdWlsZEVycm9ycyI6ZmFsc2UsInRzY29uZmlnUGF0aCI6InRzY29uZmlnLmpzb24ifSwiZGlzdERpciI6Ii5uZXh0IiwiY2xlYW5EaXN0RGlyIjp0cnVlLCJhc3NldFByZWZpeCI6IiIsImNvbmZpZ09yaWdpbiI6Im5leHQuY29uZmlnLmpzIiwidXNlRmlsZVN5c3RlbVB1YmxpY1JvdXRlcyI6dHJ1ZSwiZ2VuZXJhdGVFdGFncyI6dHJ1ZSwicGFnZUV4dGVuc2lvbnMiOlsidHN4IiwidHMiLCJqc3giLCJqcyJdLCJwb3dlcmVkQnlIZWFkZXIiOnRydWUsImNvbXByZXNzIjp0cnVlLCJhbmFseXRpY3NJZCI6IiIsImltYWdlcyI6eyJkZXZpY2VTaXplcyI6WzY0MCw3NTAsODI4LDEwODAsMTIwMCwxOTIwLDIwNDgsMzg0MF0sImltYWdlU2l6ZXMiOlsxNiwzMiw0OCw2NCw5NiwxMjgsMjU2LDM4NF0sIn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ZWFjdC1pY29ucy9yaSIsInJlYWN0LWljb25zL3J4IiwicmVhY3QtaWNvbnMvc2kiLCJyZWFjdC1pY29ucy9zbCIsInJlYWN0LWljb25zL3RiIiwicmVhY3QtaWNvbnMvdGZpIiwicmVhY3QtaWNvbnMvdGkiLCJyZWFjdC1pY29ucy92c2MiLCJyZWFjdC1pY29ucy93aSJdfSwiY29uZmlnRmlsZSI6Ii9vcHQvYnVpbGRob21lL3JlcG8vbmV4dC5jb25maWcuanMiLCJjb25maWdGaWxlTmFtZSI6Im5leHQuY29uZmlnLmpzIn0=","pagesType":"pages","appDirLoader":"","sriEnabled":false,"middlewareConfig":"e30="}!
2023-11-04T15:13:12.750814Z ▲  
2023-11-04T15:13:12.75097Z  ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:21:0
2023-11-04T15:13:12.751122Z ▲  Module not found: Can't resolve 'path'
2023-11-04T15:13:12.751289Z ▲  
2023-11-04T15:13:12.75146Z  ▲  https://nextjs.org/docs/messages/module-not-found
2023-11-04T15:13:12.751596Z ▲  
2023-11-04T15:13:12.751721Z ▲  Import trace for requested module:
2023-11-04T15:13:12.751843Z ▲  ./node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js
2023-11-04T15:13:12.751964Z ▲  ./src/pages/api/auth/[...nextauth].ts
2023-11-04T15:13:12.752172Z ▲  ./src/pages/profile.tsx
2023-11-04T15:13:12.752369Z ▲  ./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-ssr-loader/index.js
2023-11-04T15:13:12.752598Z ▲  
2023-11-04T15:13:12.752752Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/pre-binding.js:6:0
2023-11-04T15:13:12.752892Z ▲  Module not found: Can't resolve 'fs'
2023-11-04T15:13:12.753026Z ▲  
2023-11-04T15:13:12.753165Z ▲  https://nextjs.org/docs/messages/module-not-found
2023-11-04T15:13:12.753319Z ▲  
2023-11-04T15:13:12.753456Z ▲  Import trace for requested module:
2023-11-04T15:13:12.753584Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
2023-11-04T15:13:12.753714Z ▲  ./node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js
2023-11-04T15:13:12.753848Z ▲  ./src/pages/api/auth/[...nextauth].ts
2023-11-04T15:13:12.753971Z ▲  ./src/pages/profile.tsx
2023-11-04T15:13:12.754148Z ▲  ./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-ssr-loader/index.js
2023-11-04T15:13:12.754361Z ▲  
2023-11-04T15:13:12.754912Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/pre-binding.js:6:0
2023-11-04T15:13:12.755084Z ▲  Module not found: Can't resolve 'path'
2023-11-04T15:13:12.755235Z ▲  
2023-11-04T15:13:12.755463Z ▲  https://nextjs.org/docs/messages/module-not-found
2023-11-04T15:13:12.755598Z ▲  
2023-11-04T15:13:12.755726Z ▲  Import trace for requested module:
2023-11-04T15:13:12.756386Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
2023-11-04T15:13:12.756559Z ▲  ./node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js
2023-11-04T15:13:12.756739Z ▲  ./src/pages/api/auth/[...nextauth].ts
2023-11-04T15:13:12.756877Z ▲  ./src/pages/profile.tsx
2023-11-04T15:13:12.757105Z ▲  ./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-ssr-loader/index.js
2023-11-04T15:13:12.757336Z ▲  
2023-11-04T15:13:12.757472Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/util/napi.js:3:0
2023-11-04T15:13:12.757622Z ▲  Module not found: Can't resolve 'fs'
2023-11-04T15:13:12.758159Z ▲  
2023-11-04T15:13:12.758338Z ▲  https://nextjs.org/docs/messages/module-not-found
2023-11-04T15:13:12.758522Z ▲  
2023-11-04T15:13:12.758671Z ▲  Import trace for requested module:
2023-11-04T15:13:12.758807Z ▲  ./node_modules/.pnpm/@[email protected]/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
2023-11-04T15:13:12.758965Z ▲  ./node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js
2023-11-04T15:13:12.759607Z ▲  ./src/pages/api/auth/[...nextauth].ts
2023-11-04T15:13:12.759779Z ▲  ./src/pages/profile.tsx
2023-11-04T15:13:12.759975Z ▲  ./node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected]/node_modules/next/dist/build/webpack/loaders/next-edge-ssr-loader/index.js
2023-11-04T15:13:12.760197Z ▲  
2023-11-04T15:13:12.760344Z ▲  > webpack config.resolve.alias was incorrectly overridden. https://nextjs.org/docs/messages/invalid-resolve-alias
2023-11-04T15:13:12.86641Z  ▲  ELIFECYCLE  Command failed with exit code 1.
2023-11-04T15:13:12.90945Z  ▲  Error: Command "pnpm run build" exited with 1
2023-11-04T15:13:12.933458Z 
2023-11-04T15:13:12.933984Z ⚡️ The Vercel build (`npx vercel build`) command failed. For more details see the Vercel logs above.
2023-11-04T15:13:12.934379Z ⚡️ If you need help solving the issue, refer to the Vercel or Next.js documentation or their repositories.
2023-11-04T15:13:12.934679Z 
2023-11-04T15:13:12.95764Z  Failed: Error while executing user command. Exited with error code: 1
2023-11-04T15:13:12.968836Z Failed: build command exited with code: 1
2023-11-04T15:13:15.960823Z Failed: error occurred while running build command

The problem is that, while it solved the fs and path errors, it created more errors for other missing modules.
I don’t know what I should do to make the build work.