Cannot perform I/O on behalf of a different request

I'm trying to use the postgres library with neon.tech but I get this error:
Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Writable)
Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Writable)
import postgres from 'postgres';
import { DATABASE_URL } from '$env/static/private';

export const sql = postgres(DATABASE_URL);
import postgres from 'postgres';
import { DATABASE_URL } from '$env/static/private';

export const sql = postgres(DATABASE_URL);
1 Reply
James
James3w ago
you won't be able to initiate postgres like this outside of a fetch (etc.) handler a common approach is to create a lib that you import inside your handler, then pass that instance around to your other methods, or store it in something like AsyncLocalStorage

Did you find this page helpful?