Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#14549 closed bug (fixed)

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

Reported by: domenic@… Owned by: Timmy Willison
Priority: blocker Milestone: 1.11/2.1
Component: core Version:
Keywords: Cc:
Blocked by: Blocking:


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.)

Change History (4)

comment:1 Changed 10 years ago by Timmy Willison

Component: unfiledcore
Milestone: None1.11/2.1
Owner: set to Timmy Willison
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 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.

Version 0, edited 10 years ago by Timmy Willison (next)

comment:2 Changed 10 years ago by Timmy Willison

Resolution: fixed
Status: assignedclosed

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

Changeset: 8f7db68cbf1cdc3677d89dc1191bc50c8d47a82f

comment:3 Changed 10 years ago by 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?

comment:4 in reply to:  3 Changed 10 years ago by m_gol

Replying to 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?

Note: See TracTickets for help on using tickets.