I’m using Cloudflare Workers to connect a PostgreSQL database hosted in Azure.
The problem is every time, I try to run the code, I get this error:
You can mark the path “pg-native” as external to exclude it from the bundle, which will remove this error. You can also surround this “require” call with a try/catch block to handle this failure at run-time instead of bundle-time.
X [ERROR] Build failed with 1 error:
node_modules/pg/lib/native/client.js:4:21: ERROR: Could not resolve “pg-native”
I tried npm install pg-native many times, and it results with errors. From other thread I read, that pg-native can’t be bundled through esbuild. But in that case what’s the solution, or a workaround?
Here’s my code:
const { Client } = require('pg');
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const client = new Client({
user: 'XXX',
host: 'XXX',
database: ' XXX ',
password: ' XXX ',
port: 5432,
});
await client.connect();
const res = await client.query('SELECT * from XXX ');
await client.end();
return new Response(res.rows[0].message);
}