Skip to content

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

Closed

Description

@hassankhan

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'],
    }],
  },
};

Activity

  1. manueltimita commented on Mar 2, 2017

    @manueltimita

    This issue should be closed, since it's Webpack specific.

    @hassankhan, you might want to have a look at the webpack specific issue here: serverless-heaven/serverless-webpack#78.

  2. brianc commented on May 24, 2017

    @brianc
    Owner

    closing at the recommendation of @manueltimita - also I don't know, I have never bundled non-browser code with webpack.

  3. brianc commented on May 24, 2017

    @brianc
    Owner

    @hassankhan I'd be interested in any info you came up with, I can add it to the docs.

  4. added this to the docs milestone on May 24, 2017
  5. hassankhan commented on May 24, 2017

    @hassankhan
    Author

    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\//),
      ],
    
  6. brianc commented on May 24, 2017

    @brianc
    Owner
  7. craiggoldstone commented on Jun 8, 2017

    @craiggoldstone

    I've managed to successfully bundle this using [email protected] + [email protected] + [email protected] + [email protected] + [email protected] + @types/[email protected]. 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
  8. jgrund commented on Jun 17, 2017

    @jgrund

    FWIW, I got webpack to compile by adding the following to my webpack config (I don't need pg-native):

    plugins: [new webpack.IgnorePlugin(/\.\/native/, /\/pg\//)]

    This removes attempted resolution of './native' in https://github.com/brianc/node-postgres/blob/v6.2.3/lib/index.js#L95 and https://github.com/brianc/node-postgres/blob/v6.2.3/lib/index.js#L104.

  9. evbo commented on May 17, 2020

    @evbo

    As pointed out by @hassankhan, same fix here
    #838 (comment)

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

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions