Monday, June 4, 2012

Can I use jQuery with node.js?

Is it possible to use jQuery selectors/dom manipulation server-side using node.js ?

Source: Tips4all


  1. Since november 4 2010, simply:

    npm install jquery

    It will fetch the dependencies which are: jsdom and htmlparser

    Npm can be installed here

    Read the thread

  2. Using this library you now can. Just look at their jquery example in the examples directory.

  3. Yes you can, using a library I created called nodeQuery

    var Express = require('express')
    , dnode = require('dnode')
    , nQuery = require('nodeQuery')
    , express = Express.createServer();

    var app = function ($) {
    $.on('ready', function () {
    // do some stuff to the dom in real-time
    $('body').append('Hello World');
    $('body').append('<input type="text" />');
    $('input').live('click', function () {
    console.log('input clicked');
    // ...


    .use(Express.static(__dirname + '/public'))


  4. I believe the answer to this is now yes.

    var navigator = { userAgent: "node-js" };
    var jQuery = require("./node-jquery").jQueryInit(window, navigator);

  5. Jsdom is a great tool. But if you will want to evaluate "whole pages" and doing some funky stuff on them server side i suggest wrapping them into


    So things like require / common js on site will not blow your node process it self.

    more docs:


  6. No. It's going to be quite a big effort to port a browser environment to node.

    Another approach, that I'm currently investigating for unit testing, is to create "Mock" version of jQuery that provides callbacks whenever a selector is called.

    This way you could unit test your jQuery plugins without actually having a DOM. You'll still have to test in real browsers to see if your code works in the wild, but if you discover browser specific issues, you can easily "mock" those in your unit tests as well.

    I'll push something to once it's ready to show.

  7. Not that I know of. The DOM is a client side thing (jQuery doesn't parse the HTML, but the DOM).

    Here are some current Node.js projects:

    And SimonW's djangode is pretty damn cool...

  8. An alternative is to use Underscore.js. It should provide what you might have wanted server-side from JQuery.