node-postgres: when using babel, pg-native is always required (and fails if not present)
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.
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 11
- Comments: 37 (6 by maintainers)
Commits related to this issue
- https://github.com/brianc/node-postgres/issues/838 — committed to Rayan20/Angular-IOT by richard-yang-pearson-dev 4 years ago
- https://github.com/brianc/node-postgres/issues/838 — committed to Rayan20/Angular-IOT by richard-yang-pearson-dev 4 years ago
- https://github.com/brianc/node-postgres/issues/838 — committed to Rayan20/Angular-IOT by richard-yang-pearson-dev 4 years ago
- https://github.com/brianc/node-postgres/issues/838 — committed to Rayan20/Angular-IOT by richard-yang-pearson-dev 4 years ago
- https://github.com/brianc/node-postgres/issues/838 — committed to Rayan20/Angular-IOT by richard-yang-pearson-dev 4 years ago
- https://github.com/brianc/node-postgres/issues/838 — committed to Rayan20/Angular-IOT by richard-yang-pearson-dev 4 years ago
- Ignore pg-native to fix webpack bug See https://github.com/brianc/node-postgres/issues/838#issuecomment-405646013 — committed to mike-fam/tutor-timetable-v2 by mike-fam 4 years ago
Another solution (if using webpack), is to add
new webpack.IgnorePlugin(/^pg-native$/)
to your webpack config’s plugins array. E.g.Nothing worked for me, so I created a manual fix myself
create a fix.js file at the root of the project:
to run the patch file whenever you update or install a new dependency, add it to the package.json file:
If you are using sequelize, make the following configuration in the configuration part:
This might help you:
import pg from ‘pg’ does not work for me - I am seeing the pg-native error.
I also couldn’t bundle
pg
in my Typescript + Webpack2 + Lambda project until I installedpg-native
and set the webpack configoutput: 'node'
. Small example project: https://github.com/craigsnyders/serverless-typescript-starterI’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.jsonStill facing the same issue on
"pg": "^6.1.5"
.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; // }); }
I think the original issue asks about babel, which seems to be still an issue. E.g. when using
parcel
, get the same errors:@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: { ... } ... }
that’s not a fix, that’s a workaround. keyword: optional
An alternative workaround is to create a local pg-native package and reference it from your package.json
The
index.js
file in thepg-native
folder only has to containand a basic
package.json
file like this:I also get the same error with pg 7.4.3 using webpack and typescript.
is this actually fixed? or has it been closed due to workarounds?
I added following in my webpack config to workaround the issue,
Just in case anyone is using
serverless-bundle
to use webpack with the serverless framework, here’s the workaround:pg define getter in module.exports https://github.com/brianc/node-postgres/blob/master/lib/index.js#L76
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
nice it‘s worked for me thx @antonycms
Here is a workaround if you are using webpack
pg-native.js
webpack.config.js
¯\_(ツ)_/¯ anything that unblocks me is a fix.
@antonycms thanks so much! struggled with this issue and your solution works like a charm!