Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:57:58+00:00 2026-05-22T18:57:58+00:00

I am trying to build a simple jQuery plugin with node.js. I need to

  • 0

I am trying to build a simple jQuery plugin with node.js. I need to be able to access the global context, which in the browser is the window object, but haven’t figured out how yet.

Here’s the setup:

1. /spec/spec_runner.js

//fake browser window
var jsdom       = require('jsdom').jsdom,
    sys         = require('sys'),
    window      = jsdom().createWindow(),
    coffee      = require('coffee-script'),
    jasmine     = require('jasmine-node'),
    underscore  = require('underscore'),
    exec        = require('child_process').exec;
global.jQuery   = require("jquery");
global.$        = global.jQuery;
global._        = underscore;
global.window   = window;

var plugin = require("../lib/my-plugin.js");
global.Model = plugin.Model;

2. /lib/my-plugin.js

var Model = function() {
  console.log(window.document);
}

Model.prototype.doSomething = function() {
  console.log(window.document);
}

Problem is, that window attribute is not correct. I can get it to work if I do this:

Model.prototype.doSomething = function() {
  console.log(global.window.document);
}

…but that’s a problem for when I’m running this in the browser.

How do I get it so I can write the plugin and test it using node.js, but it has no (or at least a standardized) reference to global or exports?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:57:59+00:00Added an answer on May 22, 2026 at 6:57 pm

    It’s working fine for me in this simplified test case:

    scope_test.js

    var jsdom = require('jsdom').jsdom;
    var window = jsdom().createWindow();
    global.window = window;
    
    var test = require('./plugin_test.js')
    global.Model = new test.Model();
    Model.doSomething();
    

    plugin_test.js

    exports.Model = Model;
    
    function Model() {
      console.log(window.document);
    }
    
    Model.prototype.doSomething = function() {
      console.log(window.document);
    }
    

    Result:

    $ node scope_test.js 
    { _childNodes: 
       [ { _attributes: [Object],
           _ownerDocument: [Circular],
           _childNodes: [Object],
           style: [Object],
           _childrenList: [Object],
           _childNodesList: [Object],
           _nodeName: 'html',
           _tagName: 'html',
           _created: true,
    
    ....
    
    
    { _childNodes: 
       [ { _attributes: [Object],
           _ownerDocument: [Circular],
           _childNodes: [Object],
           style: [Object],
           _childrenList: [Object],
           _childNodesList: [Object],
           _nodeName: 'html',
           _tagName: 'html',
           _created: true,
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a simple jquery plugin that can take selectors as parameters.
I'm trying to build a jQuery plugin that filters the provided jQuery object to
I'm currently trying to build a simple component which should monitor, if the user
I'm trying to build a very, very simple micro-webapp which I suspect will be
I am new to Jquery and and trying to build a simple gallery. I
I'm trying to build a simple carousel animation with jQuery. There are 3 viewable
I'm trying to build a simple alert mechanism with jQuery Tools -- in response
I am trying to build a widget which requires the person to load jQuery
I am trying to build a simple jQuery UI template and populate it with
hey guys, I'm trying to build a simple custom select box with jquery. html:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.