Skip to main content

Bug Tracker

Side navigation

#14549 closed bug (fixed)

Opened November 15, 2013 04:53PM UTC

Closed November 15, 2013 10:18PM UTC

Last modified January 26, 2014 12:19AM UTC

npm jQuery does not expose the jQuery function, but instead a wierd factory

Reported by: domenic@domenicdenicola.com Owned by: timmywil
Priority: blocker Milestone: 1.11/2.1
Component: core Version:
Keywords: Cc:
Blocked by: Blocking:
Description

Even after fixing ticket #14548 myself, I was unable to use jQuery to select my DOM elements. E.g. when I did

var $ = require('jquery');

$('body').length;

I did not get back 1, but instead an error "jQuery requires a window with a document".

I am not sure what use case the factory is supporting, but perhaps if typeof window !== "undefined" that factory could be bypassed and you could just set module.exports = factory(window);. (But again, it's weird that there's different behavior for the CommonJS module vs. the AMD module.)

Attachments (0)
Change History (4)

Changed November 15, 2013 05:37PM UTC by timmywil comment:1

_comment0: The reasoning behind the factory is here: http://bugs.jquery.com/ticket/13768. Originally, we were focusing on using jQuery in node, rather than with CommonJS in the browser. Simply requiring jQuery in Node would throw an error if there was not a _global_ window with a document present. This was subverted with the factory, which allows the user to create a window (with jsdom or the like) and then pass it into the factory to make everything work without having to have an obtrusive global window object. \ \ Nonetheless, as I was saying in IRC, I think require jQuery with CommonJS in the browser will be our more common use case, thanks to browserify. I agree that if a global window is already present, we should execute the factory immediately. This would not interfere with the Node use cases.1384537102182976
component: unfiledcore
milestone: None1.11/2.1
owner: → timmywil
priority: undecidedblocker
status: newassigned

The reasoning behind the factory is here: http://bugs.jquery.com/ticket/13768. Originally, we were focusing on using jQuery in node, rather than with CommonJS in the browser. Simply requiring jQuery in Node would throw an error if there was not a _global_ window with a document present. This was subverted with the factory, which allows the user to create a window (with jsdom or the like) and then pass it into the factory to make everything work without the need for an obtrusive global window object.

Nonetheless, as I was saying in IRC, I think requiring jQuery with CommonJS in the browser will be our more common use case, thanks to browserify. I agree that if a global window is already present, we should execute the factory immediately. This would not interfere with the Node use cases.

Changed November 15, 2013 10:18PM UTC by Timmy Willison comment:2

resolution: → fixed
status: assignedclosed

Fixes #14549. Execute the factory immediately when CommonJS is used in the browser.

Changeset: 8f7db68cbf1cdc3677d89dc1191bc50c8d47a82f

Changed January 26, 2014 12:09AM UTC by anonymous comment:3

This is really bad news. So how do we now use jquery without having to use jsdom in node? How do I even use it at all?

Changed January 26, 2014 12:19AM UTC by m_gol comment:4

Replying to [comment:3 anonymous]:

This is really bad news. So how do we now use jquery without having to use jsdom in node? How do I even use it at all?

jQuery is mainly a DOM library. What would you use it for without a DOM?