Zanzibar Hero
Zanzibar Hero

Reputation: 11

Deploying Nuxt app with Postgres to Cloudflare fails

I have a Nuxt app backed by Supabase deployed on Cloudflare. It works fine.

Now I am trying to switch to Kysely for better control over database migrations. The Postgres dialect is backed by a Postgres module, either pg or postgres work locally.

When I deploy the changes to Cloudflare, I get an error when the build is prerendering. For postgres, the error is:

[error] [nitro] Error: Cannot resolve "cloudflare:sockets" from "/opt/buildhome/repo/node_modules/postgres/cf/polyfills.js" and externals are not allowed!

Something similar happens with pg, although it complains about pg-native instead.

Cannot resolve "pg-native" from "/home/dave/projects/nuclearambitions/won-on-nuxt/node_modules/pg/lib/native/client.js" and externals are not allowed!

In the latter case, the "solution" I found is to exclude pg-native from the build. I tried this:

  vite: {
    build: {
      rollupOptions: {
        external: ['pg-native'],
      },
    },
  },

but that didn't change anything.

Am I out of luck with this combination: Nuxt (Nitro), Postgres, Cloudflare? Or does anyone know the correct way to exclude externals in a Vite build?

By the way, I can cause the error if I build locally with the Cloudflare presets. From nuxt.config.ts

 nitro: {
    preset: 'cloudflare-pages',
  },

Upvotes: 1

Views: 268

Answers (1)

thaciohelmer
thaciohelmer

Reputation: 61

I had the same problem, but I was deploying to netlify.

Your solution works, but because it's server-side you have to add it to the nitro rollup.

Below is the error I had and the solution that worked for me:

error:

"6:28:49 PM: [error] Cannot resolve "cloudflare:sockets" from "/opt/build/repo/node_modules/postgres/cf/polyfills.js" and externals are not allowed! 6:28:49 PM: at Object.resolveId (node_modules/nitropack/dist/nitro.mjs:1970:17) 6:28:49 PM: at async PluginDriver.hookFirstAndGetPlugin (node_modules/rollup/dist/es/shared/node-entry.js:20630:28) 6:28:49 PM: at async resolveId (node_modules/rollup/dist/es/shared/node-entry.js:19234:26) 6:28:49 PM: at async ModuleLoader.resolveId (node_modules/rollup/dist/es/shared/node-entry.js:19663:15) 6:28:49 PM: at async Object.resolveId (node_modules/@rollup/plugin-commonjs/dist/es/index.js:588:10) 6:28:49 PM: at async PluginDriver.hookFirstAndGetPlugin (node_modules/rollup/dist/es/shared/node-entry.js:20630:28) 6:28:49 PM: at async resolveId (node_modules/rollup/dist/es/shared/node-entry.js:19234:26) 6:28:49 PM: at async ModuleLoader.resolveId (node_modules/rollup/dist/es/shared/node-entry.js:19663:15) 6:28:49 PM: at async ModuleLoader.resolveDynamicImport (node_modules/rollup/dist/es/shared/node-entry.js:20066:82) 6:28:49 PM: at async node_modules/rollup/dist/es/shared/node-entry.js:19954:32 6:28:49 PM: [error] Cannot resolve "cloudflare:sockets" from "/opt/build/repo/node_modules/postgres/cf/polyfills.js" and externals are not allowed! 6:28:49 PM: error: script "build" exited with code 1 6:28:49 PM: ​ 6:28:49 PM: "build.command" failed"

solution:

nitro: {
  preset: 'netlify-edge',
  rollupConfig: {
      external: ['postgres']
  }
}

Upvotes: 3

Related Questions