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 8265255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:44:07+00:00 2026-06-08T04:44:07+00:00

I am playing around and learning about vows with a personal project. This is

  • 0

I am playing around and learning about vows with a personal project. This is a small client side library, with testing done in vows. Therefore, I must build and test a file that is written like this:

(function(exports) { 
    var module = export.module = { "version":"0.0.1" }; 
    //more stuff
})(this);

In my testing (based off of science.js, d3, etc.) requires that module like so:

require("../module");

I continued to get a “module not defined error” when trying to run the tests, so I went to a repl and ran:

require("../module")

and it returned:

{ module: { version: "0.0.1" } }

I realize I could do something like:

var module = require("../module").module;

but feel like I am creating a problem by doing it that way, especially since the libraries that I based this project on are doing it in the format I described.

I would like for my project to behave similar to those which I based it off of, where:

require("../module");

creates a variable in this namespace:

module.version; //is valid.

I have seen this in a variety of libraries, and I am following the format and thought process to the T but believe I might be missing something about require behavior I don’t know about.

  • 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-06-08T04:44:09+00:00Added an answer on June 8, 2026 at 4:44 am

    There is no problem creating it this way. Modules define what they return in the module.exports object. By the way, you don’t actually need self executing functions (SEF), there is no global leakage like in browsers 🙂

    Examples

    module1.js:

    module.exports = {
        module: { 'version': '0.1.1' }
    };
    

    main.js:

    var module1 = require( './module1.js' );
    // module1 has what is exported in module1.js
    

    Once you’ve understood how this works, you can easily export the version number right away if you want to:

    module1.js:

    module.exports = '0.1.1';
    

    main.js:

    var module1 = require( './module1.js' );
    console.log( module1 === '0.1.1' ); // true
    

    Or if you want some logic, you can easily extend your module1.js file like this:

    module.exports = ( function() {
        // some code
        return version;
    } () ); // note the self executing part :-)
    // since it's self executed, the exported part
    // is what's returned in the SEF
    

    Or, as many modules do, if you want to export some utility functions (and keep others “private”), you could do it like this:

    module.exports = {
        func1: function() {
            return someFunc();
        },
    
        func2: function() {},
    
        prop: '1.0.0'
    };
    
    // This function is local to this file, it's not exported
    function someFunc() {
    }
    

    So, in main.js:

    var module1 = require( './module1.js' );
    module1.func1(); // works
    module1.func2(); // works
    module1.prop; // "1.0.0"
    module1.someFunc(); // Reference error, the function doesn't exist
    

    Your special case

    About your special case, I wouldn’t recommend doing it like they’re doing.

    If you look here: https://github.com/jasondavies/science.js/blob/master/science.v1.js

    You see that they’re not using the var keyword. So, they’re creating a global variable.

    This is why they can access it once they require the module defining the global variable.

    And by the way, the exports argument is useless in their case. It’s even misleading, since it actually is the global object (equivalent of window in browsers), not the module.exports object (this in functions is the global object, it’d be undefined if strict mode were enabled).

    Conclusion

    Don’t do it like they’re doing, it’s a bad idea. Global variables are a bad idea, it’s better to use node’s philosophy, and to store the required module in a variable that you reuse.

    If you want to have an object that you can use in client side and test in node.js, here is a way:

    yourModule.js:

    // Use either node's export or the global object in browsers
    var global = module ? module.exports : window.yourModule;
    
    ( function( exports ) {
        var yourModule = {};
        // do some stuff
        exports = yourModule;
    } ( global ) );
    

    Which you can shorten to this in order to avoid creating the global variable:

    ( function( exports ) {
        var yourModule = {};
        // do some stuff
        exports = yourModule;
    } ( module ? module.exports : window.yourModule ) );
    

    This way, you can use it like this on the client-side:

    yourModule.someMethod(); // global object, "namespace"
    

    And on the server side:

    var yourModule = require( '../yourModule.js' );
    yourModule.someMethod(); // local variable :-)
    

    Just FYI, .. means “parent directory”. This is the relative path of where to get the module. If the file were in the same directory, you’d use ..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently learning about basic networking in java. I have been playing around
I'm still learning about Android, and while playing around with TabHost and TabWidget ,
Just learning more about threads and concurrency, and thought of playing around with a
I recently just started playing around in PHP and got myself a small project/homework.
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Been playing around with this for a couple of hours and can't seem to
I'm learning ASP.net and I've been playing around with themes and master pages. I
I'm still learning python and after playing around with pygame I noticed I'm re-importing
I'm learning Python and I have been playing around with packages. I wanted to
I've been playing around with boost threads today as a learning exercise, and I've

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.