Skip to main content

Bug Tracker

Side navigation

#14650 closed bug (notabug)

Opened December 22, 2013 08:22PM UTC

Closed December 23, 2013 04:10PM UTC

jQuery .each broken for Node

Reported by: Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 2.1.0-beta2
Keywords: Cc:
Blocked by: Blocking:

Using 2.1.0-beta3 ''(version is not selectable from dropdown)'', I receive the following error when calling ''.each'' of the jQuery module. This was working fine with the previous version. Please let me know if I can supply any more info.



brokenjQuery.js file contents:

var $ = require('jquery');
$.each(['1','2','3','4'], function(i) {

run node brokenjQuery.js, and receive:

Object function ( w ) {
				if ( !w.document ) {
					throw new Error( "jQuery requires a window with a document" );
				return factory( w );
			} has no method 'each'
Attachments (0)
Change History (5)

Changed December 22, 2013 08:52PM UTC by dmethvin comment:1

owner: →
status: newpending

Exactly what is your use case? Are you saying you only using methods like $.each() and therefore don't need a window or document for your case?

Changed December 22, 2013 09:50PM UTC by anonymous comment:2

brokenjQuery.js above is run using node. There is no browser or window or document present, it's running on the shell or whatever environment node uses by default.

As a side note, I've reverted back to 1.8.3 to get this working locally, but wanted to point out the issue because any node developers referencing jQuery "*" in their package will get broken code when they update.



Changed December 22, 2013 10:12PM UTC by scottgonzalez comment:3

Can you please answer the question of what the use case is? Why are you using jQuery in an environment that doesn't have a window? Generally jQuery is used on the server with something like jsdom which provides the window. If all you want is utility methods, jQuery is a terrible choice.

As a side note, if you're using * for your version, you're asking for your code to break without warning.

Changed December 22, 2013 11:48PM UTC by comment:4

status: pendingnew

I've given my exact use case. When I run these 4 lines the code breaks with the error stated above.

var $ = require('jquery');
$.each(['1','2','3','4'], function(i) {

jQuery is published to npm for use in node applications.

I'll take a look at jsdom, thanks.

Changed December 23, 2013 04:10PM UTC by dmethvin comment:5

resolution: → notabug
status: newclosed

jQuery is published to npm so it can be used for DOM-related applications, either using node.js or browserify. In those situations you'd need to have a browser-like environment, which would include document and window. If you're only using the non-DOM utilities like $.each() then you should be using a non-DOM library such as lodash.js.

We're still not clear on the use case here but it doesn't seem like one we've intended to address, so I'll close this ticket. Feel free to provide more information if you'd like so we can understand.