Bug Tracker

Ticket #14549 (closed bug: fixed)

Opened 17 months ago

Last modified 14 months ago

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

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


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

comment:1 Changed 17 months ago by timmywil

  • Owner set to timmywil
  • Priority changed from undecided to blocker
  • Status changed from new to assigned
  • Component changed from unfiled to core
  • Milestone changed from None to 1.11/2.1

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.

Last edited 17 months ago by timmywil (previous) (diff)

comment:2 Changed 17 months ago by Timmy Willison

  • Status changed from assigned to closed
  • Resolution set to fixed

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

Changeset: 8f7db68cbf1cdc3677d89dc1191bc50c8d47a82f

comment:3 follow-up: ↓ 4 Changed 14 months 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 14 months 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.