Open nahuel opened 9 years ago
+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
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; // }); }
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;
}
}
This might help you:
import pg from 'pg'
Any progress with this issue?
I'm not experiencing this issue. I also using import pg from 'pg'
. This can be solution I think.
import pg from 'pg' does not work for me - I am seeing the pg-native error.
@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.
@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
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 https://github.com/brianc/node-postgres/issues/838#issuecomment-188453312 .
Still facing the same issue on "pg": "^6.1.5"
.
I'm too facing it, using Webpack and AWS Lambda.
This comment workaround worked (just the alias/pg-native.js
part):
https://github.com/elastic-coders/serverless-webpack/issues/78#issuecomment-271336996
Still having this issue bundling with webpack. Installing pg-native fixes the issue.
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
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'
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: { ... },
...
}
@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: { ... } ... }
@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
This still seems to be an issue, so unsure why it was closed
@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.
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.
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'
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,
},
};
@antonycms thanks so much! struggled with this issue and your solution works like a charm!
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"
}
is this actually fixed? or has it been closed due to workarounds?
npm install --save pg-native
fixes this error, since pg
specifies pg-native
as an optional peer dependency.
npm install --save pg-native
fixes this error, sincepg
specifiespg-native
as an optional peer dependency.
that's not a fix, that's a workaround. keyword: optional
¯\(ツ)\/¯ anything that unblocks me is a fix.
Just in case anyone is using serverless-bundle
to use webpack with the serverless framework, here's the workaround:
custom:
bundle:
ignorePackages:
- pg-native
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.
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'),
},
},
...,
nice it‘s worked for me thx @antonycms
@sdegroot Thank you, you have served me. https://github.com/edwinspire/pgapi-base/tree/main/src/server/class/pg-native
I added following in my webpack config to workaround the issue,
new IgnorePlugin({
resourceRegExp: /^pg-native$/,
contextRegExp: /./,
}),
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 thepg-native
folder only has to containmodule.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.
I'm not sure if this is a Babel or a node-postgres bug, steps to reproduce:
Babel generates the following code in
t-es5.js
: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.