3

I have installed jQuery (v1.11.1) with npm. (I need 1.x since I want to use it with Angular that does not support 2.x).

I am then trying to import the jQuery object with require via browserify, but it seems like the returned object is not the expected so I can't use it.

var jQuery = require('jquery/dist/jquery')(window);

When trying to use it I get Uncaught TypeError: object is not a function.

What am I doing wrong? How can I use jQuery with browserify?

1 Answer 1

4

jQuery has main set to dist/jquery.js, thus you can simply do this:

var jQuery = require('jquery');

jQuery('body').text('hello world');

Note that jQuery exports its factory only if there is no global document property. When used with Browserify, there is one, so what gets exported is not the factory function, but the regular jQuery object.

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

1 Comment

It worked, thank you! I can't believe it would be that simple. I wonder if this is new with 1.11. This site advised me to use require('jquery/dist/jquery')(window) on jQuery version before v2.1.0. So, apparently this is not longer correct. learnjs.io/blog/2013/11/23/backbone-jquery-browserify

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.