question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

2reactions
tylerdmacecommented, Nov 5, 2020

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.

0reactions
raulbarrigacommented, Oct 5, 2022

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 thepg package & Pool, but getting pool.query is not a constructor error. I installed npm 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 });
  }
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

webpack import error with node-postgres ('pg'.Client)
The error above was thrown when attempting to run a Cypress test that required the npm package 'pg'. Attempting to install the pg-native...
Read more >
pg-native - npm
A slightly nicer interface to Postgres over node-libpq. Latest version: 3.0.1, last published: 4 months ago. Start using pg-native in your ...
Read more >
serverless/serverless - Gitter
If you look at the pg code, it attempts to require pg-native and fails if it's not there. not fails, ignores. BUT, jaws...
Read more >
Solved: pg-native could not be resolved - Shopify Community
... although I'm still getting errors on creating the web pixel ... We spent a good chunk of time trying to fix the...
Read more >
Unable to publish function using postgres - Build Plugins
Unable to deploy this: postgres function If I remove pg dependencies, it works: "pg": "^7.4.1", "pg-native": "^2.2.0", I also try to run it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found