Skip to content

pg-native - How to bundle? #78

Closed
Closed

Description

@rentrop

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

Activity

  1. thenikso commented on Jan 9, 2017

    @thenikso
    Contributor

    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);
        });
    };
    
  2. rentrop commented on Jan 10, 2017

    @rentrop
    Author

    Perfect, thanks.

  3. vitaly-t commented on Jan 12, 2017

    @vitaly-t

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

  4. adieuadieu commented on Jul 17, 2018

    @adieuadieu

    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: { ... }
      ...
    }
    
  5. aychtang commented on Aug 11, 2019

    @aychtang

    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
  6. databyte commented on Mar 26, 2020

    @databyte

    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

  7. joinnusdev commented on Aug 15, 2020

    @joinnusdev

    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

  8. getkey commented on Nov 1, 2020

    @getkey

    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$/,
    		}),
    	],
    };
  9. alecsci commented on Jun 14, 2021

    @alecsci

    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 !

  10. bibhuticoder commented on Feb 2, 2022

    @bibhuticoder

    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

  11. yulei-chen commented on Jul 20, 2022

    @yulei-chen

    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

  12. alexporrello commented on Feb 13, 2023

    @alexporrello

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions