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 using babel, pg-native is always required (and fails if not present)

See original GitHub issue

I’m not sure if this is a Babel or a node-postgres bug, steps to reproduce:

export PATH=$PATH:/tmp/iojs-v3.0.0-linux-x64/bin/:./node_modules/.bin
mkdir t
cd t
npm install babel pg

# create a t.js file with this ES6 content:
import * as pg from "pg";
console.log(pg);

# now execute: 
babel t.js > t-es5.js
node t-es5.js 

Error: Cannot find module 'pg-native'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/tmp/o/node_modules/pg/lib/native/index.js:1:76)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

Babel generates the following code in t-es5.js:

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }

var _pg = require("pg");

var pg = _interopRequireWildcard(_pg);

console.log(pg);

The exception is happening in the _interopRequireWildcard(_pg) call. This doesn’t happens with other libraries, so I’m not sure if a Babel or a node-postgres bug.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:10
  • Comments:36 (6 by maintainers)

github_iconTop GitHub Comments

46reactions
adieuadieucommented, Sep 25, 2018

Another solution (if using webpack), is to add new webpack.IgnorePlugin(/^pg-native$/) to your webpack config’s plugins array. E.g.

const webpackConfig = {
  ...
  resolve: { ... },
  plugins: [
    new webpack.IgnorePlugin(/^pg-native$/)
  ],
  output: { ... },
  ...
}
23reactions
antonycmscommented, Sep 8, 2020

Nothing worked for me, so I created a manual fix myself

create a fix.js file at the root of the project:

const fs = require('fs');
const path = require('path');

const pgClientPath = path.resolve(__dirname, 'node_modules', 'pg', 'lib', 'native', 'client.js');

fs.readFile(pgClientPath, 'utf8', function(err, data) {
  if (err) {
    return console.log(err);
  }

  const result = data.replace("var Native = require('pg-native')", 'var Native = null');

  fs.writeFile(pgClientPath, result, 'utf8', function(err) {
    if (err) return console.log(err);
  });
});

to run the patch file whenever you update or install a new dependency, add it to the package.json file:

"scripts:": {
  "postinstall": "node fix.js"
}

If you are using sequelize, make the following configuration in the configuration part:

import * as pg from 'pg';  // import pg

const database = 'myDB';
const dialect = 'postgres';
const host = 'locahost';
const port = 5432;
const username = 'myUser';
const password = 'myPass';

export default {
  dialect,
  dialectModule: pg,
  username,
  password,
  database,
  host,
  port,

  define: {
    timestamps: true,
    underscored: true,
    underscoredAll: true,
  },
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

when using babel, pg-native is always required (and fails if not ...
I'm not sure if this is a Babel or a node-postgres bug, steps to reproduce: export PATH=$PATH:/tmp/iojs-v3.0.0-linux-x64/bin/:.
Read more >
webpack import error with node-postgres ('pg'.Client)
The problem is an interaction between the way that node-postgres is written and how babel rewrites the code, which forces pg-native to be...
Read more >
pg-native - npm
The callback is mandatory and is called with and Error if the execution failed, or with the same array of results as would...
Read more >
Setting up a Serverless Project with Webpack, Babel, and Knex
Using Webpack with the Serverless Framework is handy if you want to use ... not found: Error: Can't resolve 'pg-native' Module not found: ......
Read more >
API - esbuild
If you are using JavaScript be sure to check out the JS-specific details section ... if no input files are provided and the...
Read more >

github_iconTop Related Medium Post

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