Bug Tracker

Opened 6 years ago

Closed 6 years ago

#14650 closed bug (notabug)

jQuery .each broken for Node

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

Description

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.

Cheers, Denise

brokenjQuery.js file contents:

var $ = require('jquery');
$.each(['1','2','3','4'], function(i) {
	console.log(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'

Change History (5)

comment:1 Changed 6 years ago by dmethvin

Owner: set to Piper0804@…
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?

comment:2 Changed 6 years ago by anonymous

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.

Cheers, Denise

comment:3 Changed 6 years ago by scottgonzalez

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.

comment:4 Changed 6 years ago by Piper0804@…

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) {
	console.log(i);
});

jQuery is published to npm for use in node applications.

I'll take a look at jsdom, thanks.

comment:5 Changed 6 years ago by dmethvin

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.

Note: See TracTickets for help on using tickets.