Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when using babel, pg-native is always required (and fails if not present) #838

Open
nahuel opened this issue Aug 18, 2015 · 37 comments
Open
Milestone

Comments

@nahuel
Copy link

nahuel commented Aug 18, 2015

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.

@olegsmetanin
Copy link

+1 using webpack + babel + typescript
ERROR in .//pg/lib/native/index.js
Module not found: Error: Cannot resolve module 'pg-native' in /Volumes/DATA/SOA/2015/github/typescript_babel_react_express_dev/node_modules/pg/lib/native
@ ./
/pg/lib/native/index.js 1:13-33

@olegsmetanin
Copy link

comment this lines in node_modules/pg/lib/index.js
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
//module.exports = new PG(require('./native'));
} else {
module.exports = new PG(Client);

//lazy require native module...the native module may not have installed
// module.exports.defineGetter("native", function() {
// delete module.exports.native;
// module.exports.native = new PG(require('./native'));
// return module.exports.native;
// });
}

@artem1
Copy link

artem1 commented Oct 16, 2015

pg define getter in module.exports
https://github.com/brianc/node-postgres/blob/master/lib/index.js#L76

  //lazy require native module...the native module may not have installed
  module.exports.__defineGetter__("native", function() {
    delete module.exports.native;
    module.exports.native = new PG(require('./native'));
    return module.exports.native;
  });

when babel rewrite code, him not found __esModule property in module and make copy of module props to new hash. in copy process pg.native called unintentionally. has no idea, how to fix this

function _interopRequireWildcard(obj) { //obj is pg module
  if (obj && obj.__esModule) {  //<===== test to something
    return obj; 
  } else { 
    var newObj = {}; 
    if (obj != null) { 
      for (var key in obj) { 
        if (Object.prototype.hasOwnProperty.call(obj, key)) 
          newObj[key] = obj[key];  //<== call getter here
      } 
    } 
    newObj["default"] = obj; 
    return newObj; 
  } 
}

@thaumant
Copy link

This might help you:

import pg from 'pg'

@jmont
Copy link

jmont commented Feb 22, 2016

Any progress with this issue?

@langpavel
Copy link
Contributor

I'm not experiencing this issue. I also using import pg from 'pg'. This can be solution I think.

@nybble73
Copy link

import pg from 'pg' does not work for me - I am seeing the pg-native error.

@langpavel
Copy link
Contributor

@nybble73 What code generates babel for `import pg from 'pg'? Which versions?

This issue can be fixed only by wrapping pg.native getter to try/catch.

@langpavel
Copy link
Contributor

@brianc Should I prepare pull request?

My suggestion is return null if require('pg-native') fail

Another option is return this itself and write once an error to stdout

@brianc
Copy link
Owner

brianc commented Feb 25, 2016

sure thing - pull request always welcome!

On Wed, Feb 24, 2016 at 3:08 PM, Pavel Lang notifications@github.com
wrote:

@brianc https://github.com/brianc Should I prepare pull request? My
suggestion is return null if require('pg-native') fail


Reply to this email directly or view it on GitHub
#838 (comment)
.

@swarajgiri
Copy link

Still facing the same issue on "pg": "^6.1.5".

@catamphetamine
Copy link

catamphetamine commented Apr 14, 2017

I'm too facing it, using Webpack and AWS Lambda.
This comment workaround worked (just the alias/pg-native.js part):
serverless-heaven/serverless-webpack#78 (comment)

@ughitsaaron
Copy link

Still having this issue bundling with webpack. Installing pg-native fixes the issue.

@craiggoldstone
Copy link

I also couldn't bundle pg in my Typescript + Webpack2 + Lambda project until I installed pg-native and set the webpack config output: 'node'. Small example project: https://github.com/craigsnyders/serverless-typescript-starter

I'm also unclear if this is an issue with node-postgres or webpack. Normally webpack will resolve all dependant modules perfectly.
For reference, here's the webpack error when I haven't got pg-native in my package.json

ERROR in ./~/pg/lib/native/index.js
Module not found: Error: Can't resolve 'pg-native' in '/Users/csnyders/workspace/serverless-typescript-starter/node_modules/pg/lib/native'
 @ ./~/pg/lib/native/index.js 9:13-33
 @ ./~/pg/lib/index.js
 @ ./src/hello.ts
 @ ./src/handler.ts

@Kuchiriel
Copy link

Kuchiriel commented Jul 4, 2017

See this: serverless-heaven/serverless-webpack#78

@jonaskello
Copy link

I also get the same error with pg 7.4.3 using webpack and typescript.

 ModuleNotFoundError: Module not found: Error: Can't resolve 'pg-native' in '/xxx/node_modules/pg/lib/native'

@adieuadieu
Copy link

adieuadieu commented Jul 17, 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: { ... },
  ...
}

@amit-dhara
Copy link

amit-dhara commented Sep 24, 2018

@adieuadieu 's solution worked. need to make sure you have installed webpack and imported in config file. Also, give and comma ( , ) after "plugin [ ... ]" and before "output:"

npm install --save webpack

var webpack = require('webpack'); const webpackConfig = { ... resolve: { ... }, plugins: [ new webpack.IgnorePlugin(/^pg-native$/) ], output: { ... } ... }

@marcoippolito
Copy link

@brianc @amit-dhara @adieuadieu
I'm experiencing the same problem.
I installed

"pg": "^8.0.2",
"pg-hstore": "^2.3.3",
"sequelize": "^5.21.6",

npm@6.14.4 and Node: 12.15.0

this is the pg.js I'm using:

const { Pool } = require('pg');
const pool = new Pool();
module.exports = {
  query: (text, params, callback) => {
    return pool.query(text, params, callback);
  },
}

I also specified in webpack.config.js :

{
    "mode": "development",
    plugins: [
      new webpack.IgnorePlugin(/^pg-native$/, /^dns$)
    ],

When compiling:

npm run serve
 ERROR  Failed to compile with 2 errors                                         

These dependencies were not found:

* dns in ./node_modules/pg/lib/connection-parameters.js
* pg-native in ./node_modules/pg/lib/native/client.js

To install them, you can run: npm install --save dns pg-native

How to solve the problem?

Marco

@GeorgeWL
Copy link

This still seems to be an issue, so unsure why it was closed

@charmander
Copy link
Collaborator

@GeorgeWL The original issue was closed with a fix. If you’re seeing something new or something that appears to be the same, please include details.

@GeorgeWL
Copy link

Seeing the same, using this alongside webpack, webpack fails to correctly compile it when using TypeScript.

The workaround of adding pg-native to a webpack ignorePlugin still seems to fix it.

@moltar
Copy link

moltar commented Jun 16, 2020

I think the original issue asks about babel, which seems to be still an issue. E.g. when using parcel, get the same errors:

🚨  /.../node_modules/pg/lib/native/client.js:11:21: Cannot resolve dependency 'pg-native'

@antonycms
Copy link

antonycms commented Aug 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,
  },
};

@kickthedragon
Copy link

@antonycms thanks so much! struggled with this issue and your solution works like a charm!

Rayan20 pushed a commit to Rayan20/Angular-IOT that referenced this issue Aug 31, 2020
Rayan20 pushed a commit to Rayan20/Angular-IOT that referenced this issue Aug 31, 2020
Rayan20 pushed a commit to Rayan20/Angular-IOT that referenced this issue Aug 31, 2020
Rayan20 pushed a commit to Rayan20/Angular-IOT that referenced this issue Aug 31, 2020
Rayan20 pushed a commit to Rayan20/Angular-IOT that referenced this issue Aug 31, 2020
Rayan20 pushed a commit to Rayan20/Angular-IOT that referenced this issue Aug 31, 2020
@sdegroot
Copy link

sdegroot commented Oct 2, 2020

An alternative workaround is to create a local pg-native package and reference it from your package.json

{
  "name": "your-module",
  "private": true,
  "dependencies": {
    "pg": "^8.3.3",
    "pg-native": "file:./modules/pg-native",
  }
}

The index.js file in the pg-native folder only has to contain

module.exports = null;

and a basic package.json file like this:

{
  "name": "pg-native",
  "private": true,
  "main": "index.js"
}

@GeorgeWL
Copy link

GeorgeWL commented Oct 2, 2020

is this actually fixed? or has it been closed due to workarounds?

@jaylattice
Copy link

jaylattice commented Dec 2, 2020

npm install --save pg-native fixes this error, since pg specifies pg-native as an optional peer dependency.

@GeorgeWL
Copy link

GeorgeWL commented Dec 2, 2020

npm install --save pg-native fixes this error, since pg specifies pg-native as an optional peer dependency.

that's not a fix, that's a workaround. keyword: optional

@jaylattice
Copy link

¯\_(ツ)_/¯ anything that unblocks me is a fix.

mike-fam added a commit to mike-fam/tutor-timetable-v2 that referenced this issue Dec 11, 2020
@dj-doMains
Copy link

Just in case anyone is using serverless-bundle to use webpack with the serverless framework, here's the workaround:

custom:
  bundle:
    ignorePackages:
      - pg-native

@brianc brianc added this to the pg@9.0 milestone Jun 15, 2021
@brianc
Copy link
Owner

brianc commented Jun 15, 2021

The old api of using a getter to require the native bindings was not a good idea, but it's been around for ages and ages. Unfortunately it's a backwards compatiblity break to change it but one I'm happy to do - I've put this into the 9.0 milestone so it'll go out w/ our next major version release where we break backwards compatibility.

@brianc brianc reopened this Jun 15, 2021
@louisbuchbinder
Copy link

Here is a workaround if you are using webpack

pg-native.js

module.exports = null;

webpack.config.js

   ...,
    resolve: {
      alias: {
        'pg-native': path.join(__dirname, 'pg-native'),
      },
    },
    ...,

@zhangyuwu123
Copy link

nice it‘s worked for me thx @antonycms

@edwinspire
Copy link

@larshp
Copy link
Contributor

larshp commented Mar 22, 2022

I added following in my webpack config to workaround the issue,

    new IgnorePlugin({
      resourceRegExp: /^pg-native$/,
      contextRegExp: /./,
    }),

@merlindru
Copy link

An alternative workaround is to create a local pg-native package and reference it from your package.json

{
  "name": "your-module",
  "private": true,
  "dependencies": {
    "pg": "^8.3.3",
    "pg-native": "file:./modules/pg-native",
  }
}

The index.js file in the pg-native folder only has to contain

module.exports = null;

and a basic package.json file like this:

{
  "name": "pg-native",
  "private": true,
  "main": "index.js"
}

Thank you!!! Easiest way to do it, no webpack required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests