I'm using browserify to bundle my front-end javascript using CommonJS-style dependencies. For example, I have:
$ = require('jquery/dist/jquery'); // v2.1.0-beta2
_ = require('underscore');
Backbone = require('backbone');
However, when browserify bundles the dependencies I run into the following console error:
Error: jQuery requires a window with a document
Looking at the jQuery code, I see it's trying to use this
for the global window
.
(function( window, factory ) {
....
}(this, function( window ) {
Since browserify wraps all dependencies, this
is an object
, not the window
.
What's interesting is jQuery >= 2 should be CommonJS compatible. However, the problem is how browserify wraps the dependencies. Has anyone solved this problem?