与 remix cloudflare 模板一起使用时,Vite 无法捆绑 node-postgres (`pg`)

问题描述 投票:0回答:1

在尝试将 prisma/postgres 与我的 remix cloudflare 应用程序一起使用时,我遇到了一个奇怪的问题。这里有更多细节。预先感谢您提供的任何帮助。

复制

使用 cloudflare 模板生成混音站点:

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

该网站应该可以正常部署。如果你运行

pnpm run build && pnpm run start
,它应该可以在本地提供混音包。然后尝试安装 prisma/postgres 依赖项:

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

使用以下内容创建

app/db.server.ts

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 };

使用以下内容创建

prisma/schema.prisma

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

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

model Counter {
  count     Int      @id
}

将您环境中的

DATABASE_URL
设置为有效的 postgres 连接字符串。使用
pnpx prisma migrate reset -f
生成数据库。然后尝试使用
import { prisma } from '~/db.server';
将 prisma 客户端导入到您的 _index.tsx 文件中。在开发模式下本地运行数据库查询 (
pnpm run dev
) 工作正常,但如果您尝试部署到 cloudflare 或在开发模式下运行 wrangler,您会收到几个似乎与
pg
适配器有关的错误。

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/[email protected]/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/[email protected]/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/[email protected][email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/node_modules/pg-cloudflare/dist/index.js:1:29: ERROR:
  Could not resolve "events"
  ../node_modules/.pnpm/[email protected]/node_modules/pg-connection-string/index.js:76:77:
  ERROR: Could not resolve "fs"
  ../node_modules/.pnpm/[email protected][email protected]/node_modules/pg-pool/index.js:2:29: ERROR: Could not
  resolve "events"
  ../node_modules/.pnpm/[email protected]/node_modules/pg-protocol/dist/parser.js:9:41: ERROR: Could
  not resolve "assert"
  ../node_modules/.pnpm/[email protected]/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

使用的包管理器

pnpm

预期行为

我希望能够在 vite/remix 中捆绑 pg 而不会出现错误。

实际行为

我遇到了某种捆绑错误(如上所述)。

javascript prisma cloudflare node-postgres remix.run
1个回答
0
投票

有同样的问题 - 20 个错误,大部分是在使用提供的 remix 模板部署到 cloudflare 时与 pg 相关的

© www.soinside.com 2019 - 2024. All rights reserved.