Skip to content

pg-native - How to bundle? #78

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

Closed
rentrop opened this issue Jan 7, 2017 · 12 comments
Closed

pg-native - How to bundle? #78

rentrop opened this issue Jan 7, 2017 · 12 comments

Comments

@rentrop
Copy link

rentrop commented Jan 7, 2017

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

  1. How to handle require(<local dir>) serverless/serverless-runtime-babel#8 by @thenikso
  2. Can this package be bundled with Webpack for a Node project? brianc/node-postgres#1187

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
@thenikso
Copy link
Contributor

thenikso commented Jan 9, 2017

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);
    });
};

@rentrop
Copy link
Author

rentrop commented Jan 10, 2017

Perfect, thanks.

@rentrop rentrop closed this as completed Jan 10, 2017
@vitaly-t
Copy link

vitaly-t commented Jan 12, 2017

While pg-promise fully supports pg-native, the real performance gain is negligible, especially under the new versions of Node.js

@adieuadieu
Copy link

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: { ... }
  ...
}

@aychtang
Copy link

aychtang commented Aug 11, 2019

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.
  • Change plugin used in serverless.yml to serverless-bundle-no-pg-native.
  • Accept you are using a mid 2019 version of serverless-bundle

@databyte
Copy link

Just came across this, if you're using serverless-bundle, add this to your serverless.yml

custom:
  bundle:
    ignorePackages:
      - pg-native

See: https://www.npmjs.com/package/serverless-bundle#pg

@joinnusdev
Copy link

Just came across this, if you're using serverless-bundle, add this to your serverless.yml

custom:
  bundle:
    ignorePackages:
      - pg-native

See: https://www.npmjs.com/package/serverless-bundle#pg

that works for me, thanks

@getkey
Copy link

getkey commented Nov 1, 2020

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$/,
		}),
	],
};

@alecsci
Copy link

alecsci commented Jun 14, 2021

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$/,
		}),
	],
};

Thanks @getkey. It worked !

@bibhuticoder
Copy link

Just came across this, if you're using serverless-bundle, add this to your serverless.yml

custom:
  bundle:
    ignorePackages:
      - pg-native

See: https://www.npmjs.com/package/serverless-bundle#pg

Thanks. Saved my day

@yulei-chen
Copy link

If you are using serverless-esbuild, add this to your serverless.yml:

custom:
  esbuild:
    exclude:
      - pg-native

See: https://www.npmjs.com/package/serverless-esbuild#configuration

@alexporrello
Copy link

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$/,
		}),
	],
};

You're the best! Thank you!

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