Using Mongoose with webpack

I’m using:

  • Webpack v2.1.0-beta.27
  • Webpack-dev-server 2.1.0-beta.11
  • Mongoose 4.6.5

Do you want to request a feature or report a bug?
bug (this might be a feature request though, not sure, depending on if this functionality is deemed to be more of a problem w/ mongoose than it is with webpack)

What is the current behavior? / how to reproduce
This problem will most certainly extend beyond Mongoose to any library that attempts to load a binary file and Webpack, in its attempt to walk the dependency graph, tries to resolve this dependency.

If you load mongoose in the browser (by either doing import mongoose from 'mongoose' or require(‘mongoose’), webpack will complain:

WARNING in ./~/mongoose/lib/drivers/index.js
8:11 Critical dependency: the request of a dependency is an expression

If we look at this file in mongoose 4.6.5:

/*!
 * ignore
 */

var driver;

if (typeof window === 'undefined') {
  driver = require(global.MONGOOSE_DRIVER_PATH || './node-mongodb-native');
} else {
  driver = require('./browser');
}

/*!
 * ignore
 */

module.exports = driver;

I’m not an expert on how webpack works under the hood, but my understanding is that since it’s performing static analysis, webpack can’t really walk down the 2nd dependency path in this if-else block, so it attempts to resolve dependencies in ./node-mongodb-native, which imports a binary.

So either:

  1. Since the way webpack goes about its business is through static analysis, i get that these types of lower-level requires are not really compatible. But is there a way to let webpack know to ignore one of these resolution paths?

  2. is there a way to get webpack to ignore a nested dependency (i.e. I want webpack to ignore the mongodb binary of mongoose but still resolve everything else.

Also, how does webpack resolve other dependencies that use this type of check to see if it’s being imported in the browser or not?

If the current behavior is a bug, please provide the steps to reproduce.

All you need to do is create a simple app that imports mongoose into it and then run it through webpack.

Please mention other relevant information such as the browser version, Node.js version, Operating System and programming language.
Browser: Chrome 54.0.2840.98 (64-bit)
OS: Mac OSX El Capitan 10.11.4 (15E65)
Node.js: 5.6.0
Programming Language: JS

Author: Fantashit

2 thoughts on “Using Mongoose with webpack

  1. Disclaimer: I assume that you’re trying to build a universal JavaScript app.

    If you see that webpack is bundling express or mongoose, I would try to decouple your server from your actual presentation layer:

    • The server (containing all route handlers and all node-only stuff like the express app, db handlers, etc.) should not be bundled with webpack because usually you don’t have any benefits from webpack here.

    • Your presentation layer (usually your React/Angular app) should be bundled with webpack because this code will also be executed on the client. In this case you want the environment to be the same.

Comments are closed.