Importing pg.Client via var Client = require('pg').Client; throws the following error:
Module not found: Error: Cannot resolve module 'pg-native' in .../node_modules/pg/lib/native
@ ./~/pg/lib/native/index.js 9:13-33
How can i fix this? I found the following somewhat related Issues/answers
System-Info:
__.babelrc__
{
"plugins": ["transform-runtime"],
"presets": ["es2015", "stage-0"],
"ignore": [
"node_modules/pg/lib/native/",
"node_modules/pg/lib/native/index.js",
"node_modules/pg/lib/native/result.js",
"node_modules/pg/lib/native/query.js",
"./node_modules/pg/lib/native/",
"./node_modules/pg/lib/native/index.js",
"./node_modules/pg/lib/native/result.js",
"./node_modules/pg/lib/native/query.js",
"~/pg/lib/native/index.js",
"node_modules/pg/",
"node_modules/pg/",
"./node_modules/pg/",
"./node_modules/pg/",
"./node_modules/pg/"
]
}
__webpack.config.js__
Sames as in https://github.com/elastic-coders/serverless-webpack/blob/master/examples/babel/webpack.config.js
Your Environment Information -----------------------------
OS: darwin
Node Version: 4.3.2
Serverless Version: 1.5.0
I'll soon work on a peace of software which uses pg-promise that also imports pg-native. I fixed this some time ago but it probably needs refinement. As it was I had this in the resolve section of my webpack.config.js:
alias: {
'pg-native': path.join(__dirname, 'aliases/pg-native.js'),
'pgpass$': path.join(__dirname, 'aliases/pgpass.js'),
},
And the files are:
// alias/pg-native.js
export default null;
// alias/pgpass.js
'use strict';
var fs = require('fs')
, helper = require( 'pgpass/lib/helper.js' )
;
module.exports.warnTo = helper.warnTo;
module.exports = function(connInfo, cb) {
var file = helper.getFileName();
fs.stat(file, function(err, stat){
if (err || !helper.usePgPass(stat, file)) {
return cb(undefined);
}
var st = fs.createReadStream(file);
helper.getPassword(connInfo, st, cb);
});
};
Perfect, thanks.
While pg-promise fully supports pg-native, the real performance gain is negligible, especially under the new versions of Node.js
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: { ... }
...
}
For those experiencing this using serverless-bundle, and not wanting to migrate to serverless-webpack for their project. I have made this package for now: "serverless-bundle-no-pg-native": "1.3.0" which applies @thenikso's fix to the serverless-bundle repo. This will allow you to deploy your lambda function using pg by swapping the serverless-bundle plugin to serverless-bundle-no-pg-native
You can check the diff at https://github.com/aychtang/serverless-bundle. I will not be maintaining this so it will stay at the current version of serverless-bundle, but I thought it'd be useful to share if people are doing some small projects and want to save some time.
TLDR:
npm i serverless-bundle-no-pg-native --save-dev.serverless-bundle-no-pg-native.Just came across this, if you're using serverless-bundle, add this to your serverless.yml
custom:
bundle:
ignorePackages:
- pg-native
Just came across this, if you're using
serverless-bundle, add this to your serverless.ymlcustom: bundle: ignorePackages: - pg-native
that works for me, thanks
If you are using Webpack 5 the API of IgnorePlugin changed so @adieuadieu's code needs a little tweak.
const { IgnorePlugin } = require('webpack');
module.exports = {
plugins: [
new IgnorePlugin({
resourceRegExp: /^pg-native$/,
}),
],
};
If you are using Webpack 5 the API of
IgnorePluginchanged so @adieuadieu's code needs a little tweak.const { IgnorePlugin } = require('webpack'); module.exports = { plugins: [ new IgnorePlugin({ resourceRegExp: /^pg-native$/, }), ], };
Thanks @getkey. It worked !
Most helpful comment
If you are using Webpack 5 the API of
IgnorePluginchanged so @adieuadieu's code needs a little tweak.