question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can this package be bundled with Webpack for a Node project?

See original GitHub issue

Hi there,

I’m trying to build a Webpack bundle for an AWS Lambda project and I’m running into a few errors. I have a common package and a project package. The common package sets up a connection and returns a client from pg. The project package uses the client to actually execute queries. Currently I’m trying to bundle pg in the common package, but I’ve also tried setting it as an external with not much difference 😢 I’ve included the error I get when I try and build my project:

ERROR in ./~/server-libs/dist/bundle.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../type-overrides in /project/node_modules/server-libs/dist
 @ ./~/server-libs/dist/bundle.js 5:11534-11562

ERROR in ./~/server-libs/dist/bundle.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../../package.json in /project/node_modules/server-libs/dist
 @ ./~/server-libs/dist/bundle.js 5:11585-11614

ERROR in ./~/server-libs/dist/bundle.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../connection-parameters in /project/node_modules/server-libs/dist
 @ ./~/server-libs/dist/bundle.js 5:11688-11723

ERROR in ./~/server-libs/dist/bundle.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./query in /project/node_modules/server-libs/dist
 @ ./~/server-libs/dist/bundle.js 5:11832-11850

ERROR in ./~/server-libs/dist/bundle.js
Module not found: Error: Cannot resolve module 'pg-native' in /project/node_modules/server-libs/dist
 @ ./~/server-libs/dist/bundle.js 5:11511-11531

Webpack configuration for common package:

const path = require('path');

const PATHS = {
  dist : path.resolve(__dirname, 'dist'),
  src  : path.resolve(__dirname, 'src'),
};

module.exports = {
  entry  : './index.js',
  target : 'node',
  output : {
    path          : PATHS.dist,
    filename      : 'bundle.js',
    library       : 'ServerLib',
    libraryTarget : 'umd',
  },
  resolve : {
    extensions : ['', '.js'],
    root       : [ PATHS.src ],
  },
  module : {
    loaders : [{
      test    : /\.js$/,
      loaders : ['babel'],
      exclude : /node_modules/,
    }, {
      test    : /\.json$/,
      loaders : ['json'],
    }],
    noParse : [/pg\/lib\/native/]
  },
  externals : {
    'lodash' : true,
    'moment' : true,
  },
};

Webpack configuration for project package:

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
hassankhancommented, May 24, 2017

Hi @brianc,

I ended up using Knex so I don’t know if it’s any help, but my Webpack plugins configuration looks like this:

plugins : [
    new webpack.NormalModuleReplacementPlugin(/\.\.\/migrate/, '../util/noop.js'),
    new webpack.NormalModuleReplacementPlugin(/\.\.\/seed/, '../util/noop.js'),
    new webpack.IgnorePlugin(/mariasql/, /\/knex\//),
    new webpack.IgnorePlugin(/mssql/, /\/knex\//),
    new webpack.IgnorePlugin(/mysql/, /\/knex\//),
    new webpack.IgnorePlugin(/mysql2/, /\/knex\//),
    new webpack.IgnorePlugin(/oracle/, /\/knex\//),
    new webpack.IgnorePlugin(/oracledb/, /\/knex\//),
    new webpack.IgnorePlugin(/pg-query-stream/, /\/knex\//),
    new webpack.IgnorePlugin(/sqlite3/, /\/knex\//),
    new webpack.IgnorePlugin(/strong-oracle/, /\/knex\//),
    new webpack.IgnorePlugin(/pg-native/, /\/pg\//),
  ],
2reactions
craigsnyderscommented, Jun 8, 2017

I’ve managed to successfully bundle this using pg@6.2.3 + pg-native@1.10.1 + typescript@2.3.4 + webpack@2.6.1 + awesome-typescript-loader@3.1.3 + @types/pg@6.1.41. Made a small repo to illustrate: https://github.com/craigsnyders/serverless-typescript-starter

I think the crucial parts are:

  • npm install pg-native (webpack wasn’t including this in the output otherwise?!)
  • target: 'node' in webpack config
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to bundle your library for both NodeJS and Browser with ...
NPM Configuration. In our situation, we'll have two main files browser.js and node.js , thus can omit the following properties from package.
Read more >
Getting Started - webpack
Learn how to bundle a JavaScript application with webpack 5. ... The minimum supported Node.js version to run webpack 5 is 10.13.0 (LTS) ......
Read more >
Introduction To Webpack with Node.js - Section.io
Webpack is a static module bundler for JavaScript applications. It takes modules, whether that's a custom file that we created or something ...
Read more >
A Guide to Managing Webpack Dependencies - Toptal
The Webpack module bundler processes JavaScript code and all static assets, such as stylesheets, images, and fonts. However, configuring Webpack and its ...
Read more >
Bundling Applications with Webpack - AWS SDK for JavaScript
You can use webpack to generate bundles that run in Node.js by specifying it as a target in the configuration. ... This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found