Reputation: 3591
for example i want use jquery function extend
var object = $.extend({}, object1, object2);
But :
var $ = require('jquery');
console : $
function ( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a doc…
$.extend undefined
Upvotes: 0
Views: 382
Reputation: 6929
You can get this to work but you'll need another module called jsdom
because jQuery needs a DOM window to operate, it's not designed for Node.js. Install it with:
npm install jsdom --save
Please note: I'm using the latest jQuery 2.1.1 and jsdom 1.0.0-pre.6 both installed via npm
And then in your Node.js file e.g. app.js
:
var jsdom = require('jsdom'),
window = jsdom.jsdom().parentWindow,
$ = require('jquery')(window);
var object = $.extend({},{'foo':'bar'},{'cat':'dog'});
console.log(object);
Then in Node.js you run it:
node app.js
And see the output:
{ foo: 'bar', cat: 'dog' }
Perhaps the better option is to use something like cheerio which is a "fast, flexible, and lean implementation of core jQuery designed specifically for the server". You can install this with npm install cheerio
.
Upvotes: 3
Reputation: 18197
jquery is only for client side. For server side (node.js0, it used for unitTest scripting with browserify, ... because node.js has no window, document and DOM layer. I recommand you to use underscore.js :
npm install underscore
Underscore.js has a extend
method works like jquery.extend. :
var _ = require('underscore');
var obj = _.extend({}, object1, object2);
Upvotes: 2