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

16.7k views Asked by At

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:

4

There are 4 answers

1
Alexandre D'Erman On

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.

0
cdebro On

I know that this is an old topic but I'm compelled to share how I solved it. It was maddening to deal with.

So, here is the readers digest version as based on the recollection from the last brain cell that I have.

Error:

Webpack Compilation Error ./node_modules/pg/lib/native/client.js Module not found: Error: Can't resolve 'pg-native'

The error above was thrown when attempting to run a Cypress test that required the npm package 'pg'.

Attempting to install the pg-native package was not successful and resulted in another error; namely ->

Call to 'pg_config --libdir' returned exit status 1 while in binding.gyp. while trying to load binding.gyp

I found that executing pg_config --libdir in the VSCode cmd prompt resulted in that command failing.

However, I knew that it should work since running that command from the system command prompt resulted in this -> C:/PROGRA~1/POSTGR~1/9.3/lib

That is the path that contains a required dll.

So, instead of running npm install from the VSCode command prompt, I ran it from the command prompt as launched from windows.

The result...success!!! pg-native was installed successfully.

After, the Cypress test was able to run as well.

Errors in now way helped me to arrive at this solution. It was more just checking that things were installed that were required, etc.

0
Peter Savnik On

You may have pg-native globally installed locally. Hence the packet manager does not include the pg-native in the lock file. That was a issue i experienced where it did run fine locally but every time i build in the cloud webpack complained about pg-native missing. I solved it by removing the lockfile in the files pushed to the cloud (In this case seed.run).

0
Steve-OH On

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.