pg-native error
When I run my project where I do import pg from 'pg'; I got the following error:
Cannot find module 'pg-native'
Require stack:
Does anybody know how to fix it? I use the latest version of the node-postgres
It looks like pg-native is a peerDependency, but supposed to be marked as optional: https://github.com/brianc/node-postgres/blob/master/packages/pg/package.json#L36-L43
It looks like it's happening because when using ESM, something inside of node is walking all the properties of the module, including the .native getter defined here.
Not sure what the right answer is though. You could install pg-native knowing that it won't be used in your application to get rid of the log, or perhaps a PR with an option to disable the console.error somehow? But it looks like it's just a console.error, shouldn't actually be preventing other things from running. Super annoying though.
It depends what you need from pg module; in my case, I need just the Client (because reasons):
import Client from 'pg/lib/client.js' (if you're using ESM in Node 14.8+)
Essentially, you can copy-paste pg/lib/index.js
https://github.com/brianc/node-postgres/blob/07988f985a492c85195c6cdc928f79816af94c66/packages/pg/lib/index.js#L1-L32
into your project (make sure imports are pointing correctly) and remove the native setup:
import Client from 'pg/lib/client.js'
import Connection from 'pg/lib/connection.js'
import Pool from 'pg-pool'
import defaults from 'pg/lib/defaults.js'
import types from 'pg-types'
const poolFactory = (Client) => {
return class BoundPool extends Pool {
constructor(options) {
super(options, Client)
}
}
}
var PG = function (clientConstructor) {
this.defaults = defaults
this.Client = clientConstructor
this.Query = this.Client.Query
this.Pool = poolFactory(this.Client)
this._pools = []
this.Connection = Connection
this.types = types
}
export default new PG(Client)
I was able to work around this (using pools in my particular case) by using:
import Pool from 'pg-pool';
and installing the types using: npm i @types/pg-pool. Good enough for now, if not ideal.
Duplicate of #2358
I was able to work around this (using pools in my particular case) by using:
import Pool from 'pg-pool';and installing the types using:
npm i @types/pg-pool. Good enough for now, if not ideal. This worked for me as a guide. I was using thepgpackage &Pool, but gettingpool.query is not a constructor error.I installednpm i @types/pg& that removed the error, allowing me to receive the request.
My imports are like so:
import pg from "pg";
const pool = new pg.Pool({
user: "postgres",
password: process.env.POSTGRES_PSWD,
host: "localhost",
database: "pern-todo-app",
port: 5432,
});
export default pool;
Used it like so:
import pool from "../config/db.js";
...
export const createTodo = async (req, res) => {
try {
const { description } = req.body;
const newTodo = await pool.query(
"INSERT INTO todo (description) VALUES($1) RETURNING *",
[description]
);
res.status(201).json(newTodo.rows[0]);
} catch (error) {
res.status(409).json({ message: error.message });
}
};