Misbah Khan
Misbah Khan

Reputation: 372

using jquery with browserify

I am trying to use jQuery with browserify with the module jquery-browserify. I required the module in my client.js script as such:

    var $ = require('jquery-browserify');

and when I run my node server, after i've ran browserify, i get a "window is not defined" error. What am I doing wrong?

Upvotes: 8

Views: 11851

Answers (3)

Thorsten Lorenz
Thorsten Lorenz

Reputation: 11847

jQuery was not CommonJS compliant, i.e. it didn't export itself via module.exports = $ until 2.1.0.

Therefore you needed to shim it via browserify-shim.

browserify-shim will shim any version of jquery or any other non-CommonJS library like Zepto on the fly. Details on how to set this up are included in the readme.

As an alternative you could have also used jquery-browserify, but then you would be tied to the jQuery version that this module made CommonJS compliant.

Upvotes: 2

Chris
Chris

Reputation: 332

jQuery is now CommonJS compliant, as of version 2.1.0

Upvotes: 5

Andrew Hacking
Andrew Hacking

Reputation: 6366

Browserify can process CommonJS modules as well as AMD modules with the deamdify transform so now there should be no need to use a shim.

To be clear I only noticed AMD support in JQuery 2.0.0

Upvotes: 4

Related Questions