barrettshepherd
barrettshepherd
CDCloudflare Developers
Created by barrettshepherd on 4/17/2024 in #workers-help
Postgresql Connection (Node) erroring: No such module "cloudflare:sockets".
I've been trying to test out a CF worker with a basic DB connection and query and tried everything to debug it without any luck. I have: Node - v21.6.1 In my wrangler.toml: node_compat = true I installed 'pg' and have the newest version which should work with CF workers. Here's the basic JS I'm trying to use to connect: import { Client } from 'pg'; import { Client } from 'pg'; async function handleRequest(request) { let client = null; try { client = new Client(DB_URL); await client.connect(); const result = await client.query('SELECT * FROM TABLENAME'); return new Response(JSON.stringify(result.rows), { headers: { 'Content-Type': 'application/json' }, }); } catch (error) { console.error('Database connection error:', error); return new Response(JSON.stringify({ error: error.message }), { headers: { 'Content-Type': 'application/json' }, status: 500, }); } finally { if (client) { try { await client.end(); } catch (err) { console.error('Failed to close database connection:', err); } } } } addEventListener('fetch', (event) => { event.respondWith(handleRequest(event.request)); }); Here's my tail log: GET https://worker.barrett-2ac.workers.dev/favicon.ico - Ok @ 4/17/2024, 11:18:55 AM (error) Database connection error: Error: No such module "cloudflare:sockets". (error) Failed to close database connection: TypeError: Cannot read properties of null (reading 'close') https://worker.barrett-2ac.workers.dev I'd love any ideas or things I can try to debug. I'm hitting a wall as I can't find a ton of documentation on this online. It is a Digital Ocean Postgresql db requiring SSL, which I configured, and I think the error would differ if that was it, but I'm not too sure.
19 replies