node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Cannot find module 'pg-protocol/dist/messages' or its corresponding type declarations.

Open mbret opened this issue 6 months ago • 1 comments

the file node_modules/@types/pg/index.d.ts make use of an import that does not seem to works.

A valid import is import { NoticeMessage } from 'pg-protocol/dist/messages.js'. Maybe you changed the package.json of pg-protocol and it broke the exports

mbret avatar Jun 17 '25 20:06 mbret

I think you are referring to https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0eed2e092e4fbcfdef10559bd13f0c3401325010/types/pg/index.d.ts#L6

We need more information.

This works for me:

$ mkdir Code/np-3492
$ npm i typescript @types/pg pg tsx
// file index.ts

import { Client } from 'pg';
async function main() {
    const client = new Client({
	user: 'user',
	host: 'localhost',
	database: 'my-db',
	password: 'your_password',
	port: 5432,
    });

    try {
	await client.connect();
	const res = await client.query('SELECT 1;');
	console.log(res.rows);
    } catch (err) {
	console.error('Error executing query', err.stack);
    } finally {
	await client.end();
    }
}

main().catch(err => {
    console.error('Error in main function', err);
});
$ npx tsx index.ts
[ { '?column?': 1 } ]

hjr3 avatar Jun 19 '25 16:06 hjr3