Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite fails to bundle node-postgres (pg) properly when used with Cloudflare template #9158

Open
cayblood opened this issue Mar 29, 2024 · 1 comment

Comments

@cayblood
Copy link

cayblood commented Mar 29, 2024

Reproduction

Generate a remix site with the cloudflare template:

pnpx create-remix@latest --template remix-run/remix/templates/cloudflare

The site should deploy just fine. If you run pnpm run build && pnpm run start, it should serve up the remix bundle locally just fine. Then try to install prisma/postgres dependencies:

pnpm i pg @types/pg @prisma/adapter-pg @prisma/client

create app/db.server.ts with the following contents:

import { Pool } from 'pg';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '@prisma/client';

const connectionString = `${process.env.DATABASE_URL}`;

const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });

export { prisma };

Create prisma/schema.prisma with the following contents:

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Counter {
  count     Int      @id
}

Put your db connection string into the environment variable DATABASE_URL. Generate the database with pnpx prisma migrate reset -f. Then try to import the prisma client in your _index.tsx file with import { prisma } from '~/db.server';. Running db queries locally in dev mode (pnpm run dev) works fine, but if you try to deploy to cloudflare or to run wrangler in dev mode, you'll get several errors that seem to have to do with the pg adapter.

Compiling worker to "/Users/carl/Source/crescendo-cf-take2/.wrangler/tmp/pages-ihg4oS/functionsWorker-0.6597283384106609.mjs"...
✘ [ERROR] 20 error(s) and 0 warning(s) when compiling Worker.



✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg-cloudflare@1.1.1/node_modules/pg-cloudflare/dist/index.js:1:29:
      1 │ import { EventEmitter } from 'events';
        ╵                              ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "fs"

    ../node_modules/.pnpm/pg-connection-string@2.6.2/node_modules/pg-connection-string/index.js:76:77:
      76 │ ...cert || config.sslkey || config.sslrootcert ? require('fs') : null
         ╵                                                          ~~~~

  The package "fs" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg-pool@3.6.1_pg@8.11.3/node_modules/pg-pool/index.js:2:29:
      2 │ const EventEmitter = require('events').EventEmitter
        ╵                              ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "assert"

    ../node_modules/.pnpm/pg-protocol@1.6.0/node_modules/pg-protocol/dist/parser.js:9:41:
      9 │ const assert_1 = __importDefault(require("assert"));
        ╵                                          ~~~~~~~~

  The package "assert" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/client.js:3:27:
      3 │ var EventEmitter = require('events').EventEmitter
        ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "dns"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/connection-parameters.js:3:18:
      3 │ var dns = require('dns')
        ╵                   ~~~~~

  The package "dns" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "net"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/connection.js:3:18:
      3 │ var net = require('net')
        ╵                   ~~~~~

  The package "net" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/connection.js:4:27:
      4 │ var EventEmitter = require('events').EventEmitter
        ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "crypto"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/crypto/utils-legacy.js:5:27:
      5 │ const nodeCrypto = require('crypto')
        ╵                            ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "crypto"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/crypto/utils-webcrypto.js:1:27:
      1 │ const nodeCrypto = require('crypto')
        ╵                            ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/native/client.js:12:27:
      12 │ var EventEmitter = require('events').EventEmitter
         ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/native/query.js:3:27:
      3 │ var EventEmitter = require('events').EventEmitter
        ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "events"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/query.js:3:33:
      3 │ const { EventEmitter } = require('events')
        ╵                                  ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "net"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/stream.js:6:22:
      6 │   const net = require('net')
        ╵                       ~~~~~

  The package "net" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "tls"

    ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/stream.js:21:20:
      21 │   var tls = require('tls')
         ╵                     ~~~~~

  The package "tls" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "path"

    ../node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/helper.js:3:19:
      3 │ var path = require('path')
        ╵                    ~~~~~~

  The package "path" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "stream"

    ../node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/helper.js:4:21:
      4 │   , Stream = require('stream').Stream
        ╵                      ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "path"

    ../node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/index.js:3:19:
      3 │ var path = require('path')
        ╵                    ~~~~~~

  The package "path" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "fs"

    ../node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/index.js:4:17:
      4 │   , fs = require('fs')
        ╵                  ~~~~

  The package "fs" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Could not resolve "stream"

    ../node_modules/.pnpm/split2@4.2.0/node_modules/split2/index.js:19:30:
      19 │ const { Transform } = require('stream')
         ╵                               ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node.
  Add the "nodejs_compat" compatibility flag to your Pages project and make sure to prefix the module name with "node:" to enable Node.js compatibility.


✘ [ERROR] Build failed with 20 errors:

  ../node_modules/.pnpm/pg-cloudflare@1.1.1/node_modules/pg-cloudflare/dist/index.js:1:29: ERROR:
  Could not resolve "events"
  ../node_modules/.pnpm/pg-connection-string@2.6.2/node_modules/pg-connection-string/index.js:76:77:
  ERROR: Could not resolve "fs"
  ../node_modules/.pnpm/pg-pool@3.6.1_pg@8.11.3/node_modules/pg-pool/index.js:2:29: ERROR: Could not
  resolve "events"
  ../node_modules/.pnpm/pg-protocol@1.6.0/node_modules/pg-protocol/dist/parser.js:9:41: ERROR: Could
  not resolve "assert"
  ../node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/client.js:3:27: ERROR: Could not resolve
  "events"
  ...


🪵  Logs were written to "/Users/carl/Library/Preferences/.wrangler/logs/wrangler-2024-03-29_02-23-13_081.log"
 ELIFECYCLE  Command failed with exit code 1.
 ```

### System Info

```shell
System:
    OS: macOS 14.4
    CPU: (8) arm64 Apple M2
    Memory: 88.81 MB / 24.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 8.15.4 - ~/.nvm/versions/node/v20.11.1/bin/pnpm
  Browsers:
    Brave Browser: 123.1.64.109
    Safari: 17.4
  npmPackages:
    @remix-run/cloudflare: ^2.8.1 => 2.8.1
    @remix-run/cloudflare-pages: ^2.8.1 => 2.8.1
    @remix-run/dev: ^2.8.1 => 2.8.1
    @remix-run/react: ^2.8.1 => 2.8.1
    vite: ^5.1.0 => 5.2.6

Used Package Manager

pnpm

Expected Behavior

I expected to be able to bundle pg in vite/remix without errors.

Actual Behavior

I'm getting some sort of bundle error (detailed above).

@arjunyel
Copy link
Contributor

arjunyel commented Apr 1, 2024

@cayblood looks like you need to either "Add the "nodejs_compat" compatibility flag to your Pages project" or try Postgres.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants