Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack import error with node-postgres ('pg'.Client)

Trying to bundle the following file with Webpack fails with

ERROR in ./~/pg/lib/native/index.js Module not found: Error: Cannot resolve module 'pg-native' in .../node_modules/pg/lib/native @ ./~/pg/lib/native/index.js 9:13-33

I tried several ignore statements in the .babelrc but didnt get it running...

The test-file i want to bundle: handler.js

const Client = require('pg').Client;

console.log("done");

webpack.config.js

module.exports = {
  entry: './handler.js',
  target: 'node',
  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['babel'],
      include: __dirname,
      exclude: /node_modules/,
    }]
  }
};

.babelrc

{
  "plugins": ["transform-runtime"],
  "presets": ["es2015", "stage-1"]
}

package.json

"dependencies": {
  "postgraphql": "^2.4.0",
  "babel-runtime": "6.11.6"
},
"devDependencies": {
  "babel-core": "^6.13.2",
  "babel-loader": "^6.2.4",
  "babel-plugin-transform-runtime": "^6.12.0",
  "babel-preset-es2015": "^6.13.2",
  "babel-preset-stage-0": "^6.5.0",
  "babel-polyfill": "6.13.0",
  "serverless-webpack": "^1.0.0-rc.3",
  "webpack": "^1.13.1"
}

Somewhat related github-issues:

  • https://github.com/brianc/node-postgres/issues/1187
  • https://github.com/serverless/serverless-runtime-babel/issues/8
like image 514
Rentrop Avatar asked Jan 07 '17 14:01

Rentrop


2 Answers

This is indeed an old thread, but one that helped me nonetheless. The solution provided by Steve Schafer 1 is good, but not the simplest.

Instead, the one provided by Marco Lüthy 2 in the linked issue is probably the easiest to set up because it is pure configuration, without even the need for a dummy file to be created.

It consists of modifying your Webpack config plugins array as follows:

const webpack = require('webpack');

const webpackConfig = {
  ...
  resolve: { ... },
  plugins: [
    new webpack.IgnorePlugin(/^pg-native$/)
    // Or, for WebPack 4+:
    new webpack.IgnorePlugin({ resourceRegExp: /^pg-native$/ })
  ],
  output: { ... },
  ...
}

Updated to include a change suggested in the comments.

like image 189
Alexandre D'Erman Avatar answered Nov 16 '22 09:11

Alexandre D'Erman


This is an old thread but the problem still exists, so for anyone experiencing it, there is a workaround. The problem is an interaction between the way that node-postgres is written and how babel rewrites the code, which forces pg-native to be loaded even when you don't explicitly import/require it.

The simplest workaround is to add a couple of aliases to your webpack.config.js to cause it to link in a dummy do-nothing file instead:

{
  ...
  resolve: {
    alias: {
      ...
      'pg-native': path-to-dummy-js-file,
      'dns': path-to-dummy-js-file
    }
  }
  ...
}

where the dummy file contains a single line:

export default null

See https://github.com/brianc/node-postgres/issues/838 for further discussion and alternative workarounds.

like image 6
Steve Schafer Avatar answered Nov 16 '22 10:11

Steve Schafer