8

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?

4
  • 1
    you were trying to use this inside of node and not in a browser, right? Commented Oct 3, 2013 at 6:12
  • 2
    Browserify is to be used in a browser. Commented Mar 1, 2014 at 5:03
  • Perhaps you have to pass a reference to the window object: var $ = require('jquery-browserify')(window); Commented Mar 2, 2015 at 11:30
  • Try installing locally jquery "npm install jquery --save-dev" and use it "var $ = require('jquery');" instead of "jquery-browserify". Commented Mar 31, 2016 at 8:06

3 Answers 3

5

jQuery is now CommonJS compliant, as of version 2.1.0

Sign up to request clarification or add additional context in comments.

2 Comments

This was so long ago I don't expect a response, but whatever. How does this work. I jave jQuery 2.10 module from npm, but when I create a module and pass it in using browserify, I cannot use jquery in that module. I see a lot of comments saying it is commonJS compliant, and nothing on how to use it.
Interesting, could you post an example on GitHub? I've found var $ = require('jquery') to work pretty well.
4

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

1 Comment

Too bad this question was asked in 2012. This is probably why they made those.
2

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.

1 Comment

it is commonsJS compliant as of 2.1.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.