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

Usage in Vercel edge functions #631

Closed
ranile opened this issue Jul 6, 2023 · 5 comments
Closed

Usage in Vercel edge functions #631

ranile opened this issue Jul 6, 2023 · 5 comments

Comments

@ranile
Copy link

ranile commented Jul 6, 2023

Compiling a SvelteKit application that uses postgres.js for edge gives the following errors:

Error message
> Using @sveltejs/adapter-vercel
✘ [ERROR] Could not resolve "os"

    node_modules/postgres/src/index.js:1:15:
      1 │ import os from 'os'
        ╵                ~~~~

  The package "os" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "fs"

    node_modules/postgres/src/index.js:2:15:
      2 │ import fs from 'fs'
        ╵                ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "node:crypto"

    node_modules/ulidx/dist/esm/index.js:1:19:
      1 │ import crypto from 'node:crypto';
        ╵                    ~~~~~~~~~~~~~

  The package "node:crypto" wasn't found on the file system but is built into node. Are you trying
  to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "stream"

    node_modules/postgres/src/large.js:1:19:
      1 │ import Stream from 'stream'
        ╵                    ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "net"

    node_modules/postgres/src/connection.js:1:16:
      1 │ import net from 'net'
        ╵                 ~~~~~

  The package "net" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "tls"

    node_modules/postgres/src/connection.js:2:16:
      2 │ import tls from 'tls'
        ╵                 ~~~~~

  The package "tls" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "crypto"

    node_modules/postgres/src/connection.js:3:19:
      3 │ import crypto from 'crypto'
        ╵                    ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "stream"

    node_modules/postgres/src/connection.js:4:19:
      4 │ import Stream from 'stream'
        ╵                    ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

error during build:
Error: Build failed with 8 errors:
node_modules/postgres/src/connection.js:1:16: ERROR: Could not resolve "net"
node_modules/postgres/src/connection.js:2:16: ERROR: Could not resolve "tls"
node_modules/postgres/src/connection.js:3:19: ERROR: Could not resolve "crypto"
node_modules/postgres/src/connection.js:4:19: ERROR: Could not resolve "stream"
node_modules/postgres/src/index.js:1:15: ERROR: Could not resolve "os"
...
    at failureErrorWithLog (/home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:1636:15)
    at /home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:1048:25
    at /home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:993:52
    at buildResponseToResult (/home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:1046:7)
    at /home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:1075:16
    at responseCallbacks.<computed> (/home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:697:9)
    at handleIncomingPacket (/home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:752:9)
    at Socket.readFromStdout (/home/hamza/code/firestorm/node_modules/esbuild/lib/main.js:673:7)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)

It would be nice to be able to use postgres.js from edge functions.

Reproduction steps

Replace this runtime with edge.

Note: You'll need to add runtime: 'nodejs18.x for in this server route to suppress unrelated errors

import type { Config } from '@sveltejs/adapter-vercel';

export const config: Config = {
    runtime: 'nodejs18.x'
};
@AmyBlankenship
Copy link

This actually happens in a regular Next app as well when you import db from more than one place. It seems it re-initializes postgres, and postgres doesn't like it. What's needed is some way to create it as a Singleton, but I haven't been able to think of one that Typescript is happy with.

@porsager
Copy link
Owner

Vercel edge still doesn't support tcp connections, so not much we can do now.

I can imagine they might soon since Cloudflare added support to workers and pages.

@AmyBlankenship Mind creating a new issue for that?

@AmyBlankenship
Copy link

Vercel edge still doesn't support tcp connections, so not much we can do now.

I can imagine they might soon since Cloudflare added support to workers and pages.

@AmyBlankenship Mind creating a new issue for that?

Done #632

@porsager
Copy link
Owner

Cool @AmyBlankenship 👍

@karlhorky
Copy link
Contributor

karlhorky commented Sep 28, 2023

ohh nice looks like @Ethan-Arrowood from Vercel has started a WinterCG specification on connect() for TCP connections, maybe it becomes a cross-runtime standard 😮

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

No branches or pull requests

4 participants