Skip to content

Error: connect ECONNREFUSED When Using postgres After Upgrading to Version @sveltejs/kit@2.39.0 (or Higher) #14426

@abdelfattahradwan

Description

@abdelfattahradwan

Describe the bug

I noticed that some apps I work on, which use both SvelteKit and the postgres package, are returning 500 errors.

I dug deeper, and it seems the errors are coming from code like this (this is a fake example that is similar to the real thing):

// hooks.server.ts

const authHandle: Handle = async ({ event, resolve }) => {
  const uuid = getUserUuid(event);

  if (uuid === null || typeof uuid !== "string") {
    event.locals.userUuid = null;
    event.locals.isAuthenticated = false;
    event.locals.userPermissions = new Set();
    return resolve(event);
  }

  event.locals.userUuid = uuid;
  event.locals.isAuthenticated = true;

  const [row]: [{ permission_names: Array<string> }] = await raw.sql`
    -- Query to get the user permissions.
  `;

  event.locals.userPermissions = new Set(row?.permission_names || []);

  return resolve(event);
};

export const handle = sequence(Sentry.sentryHandle(), authHandle);

export const handleError = Sentry.handleErrorWithSentry();

The part where I query the DB throws this error:

Error: connect ECONNREFUSED 127.0.0.1:5432
	at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1637:16)
	at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)
	at cachedError (file:///home/node/app/node_modules/postgres/src/query.js:170:23)
	at new Query (file:///home/node/app/node_modules/postgres/src/query.js:36:24)
	at Object.sql (file:///home/node/app/node_modules/postgres/src/index.js:112:11)
	at authHandle (file:///home/node/app/build/server/chunks/hooks.server-B4mVD8io.js:201:30)

Downgrading to @sveltejs/kit@2.38.1 "fixes" the issue.

Also, the error is not thrown in development, only when the app is built and deployed.

Reproduction

  • Create a new SvelteKit project.
  • Add postgres package.
  • Create a hooks.server.ts file.
  • Create a custom handle function.
  • Write any query inside the function.
  • Build the app.
  • Navigate to the application URL.
  • An error 500 is returned.

Logs

Error: connect ECONNREFUSED 127.0.0.1:5432
	at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1637:16)
	at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)
	at cachedError (file:///home/node/app/node_modules/postgres/src/query.js:170:23)
	at new Query (file:///home/node/app/node_modules/postgres/src/query.js:36:24)
	at Object.sql (file:///home/node/app/node_modules/postgres/src/index.js:112:11)
	at authHandle (file:///home/node/app/build/server/chunks/hooks.server-B4mVD8io.js:201:30)

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (28) x64 Intel(R) Core(TM) i7-14700K
    Memory: 34.47 GB / 63.70 GB
  Binaries:
    Node: 22.19.0 - C:\Program Files\nodejs\node.EXE
    npm: 11.6.0 - ~\AppData\Roaming\npm\npm.CMD
    bun: 1.2.21 - ~\.bun\bin\bun.EXE
  Browsers:
    Edge: Chromium (140.0.3485.54)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    @sveltejs/adapter-node: 5.3.2 => 5.3.2 
    @sveltejs/kit: 2.39.1 => 2.39.1 
    @sveltejs/vite-plugin-svelte: ^6.2.0 => 6.2.0 
    svelte: ^5.38.10 => 5.38.10 
    vite: ^7.1.5 => 7.1.5

Severity

blocking all usage of SvelteKit

Additional Information

  • The issue can ONLY be observed when the application is built. It does NOT occur during development.
  • I double checked my environment configuration files, and everything is as it should be.
  • I tried downgrading/upgrading different packages, but only when I downgraded SvelteKit was the issue resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions