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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:02:31+00:00 2026-06-10T13:02:31+00:00

I think I’m badly misunderstanding how to use module.exports. It seems every module is

  • 0

I think I’m badly misunderstanding how to use module.exports. It seems every module is overwriting what the last one spit out.

app.js:

var express = require("express")
    , app = express()
    , routes = require('routes')
    , server = app.listen(1337, "0.0.0.0")
    , io = require('socket.io').listen(server)
    , redis = require("redis")
    , client = redis.createClient();

var moduleA = require("./moduleA")(io, client); (need to pass socket.io and redis client)

var moduleB = require("./moduleB")(io, client); (same)

moduleA.js:

module.exports = function(io, client){ 
    this.test = 'foo';
    return this;
};

moduleB.js:

module.exports = function(io, client){ 
    this.test = 'bar';
    return this;
};

back to app.js:

console.log(moduleB.test); (prints “bar”)

console.log(moduleA.test); (also prints “bar”)

Could someone explain what I’m doing wrong? I can’t think of any other way to do this, since the exports helper (?) itself doesn’t seem to accept parameters.

  • 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-10T13:02:32+00:00Added an answer on June 10, 2026 at 1:02 pm

    You are exporting a constructor. You need to construct it, not call it.

    Change

    var moduleA = require("./moduleA")(io, client);
    

    to

    var moduleA = new (require("./moduleA"))(io, client);
    

    or (for clarity)

    var ModuleA = require("./moduleA");
    var a = new ModuleA(io, client);
    

    What you are seeing happen is the usual behavior when calling constructors as functions in sloppy mode: this references the global object. So of course modifying the global object from two locations will overwrite each other, and returning this will just return the global object. You can test this yourself: with your current code,

    moduleA === moduleB === this === global
    

    One way to prevent yourself from ever shooting yourself in the foot like this again is to use strict mode instead of sloppy mode. To do so, add the line

    "use strict";
    

    at the top of every module you write (before any other code). In strict mode, this for constructors called without new is undefined, so you will get an earlier and much easier-to-understand error.

    Strict mode has many benefits of this sort; for an overview see [1], [2], [3].


    An alternate solution is to stop using constructors altogether, but instead use factory functions. This would look like:

    module.exports = function (io, client) {
        var result = {};
        result.test = "foo";
        return result;
    };
    

    You seem to be trying to do something like this anyway, since you return this even though doing so in a constructor is entirely unnecessary. You could just stop using this and use an actual object under your control, instead of one whose semantics change depending on whether your function is being called or constructed.

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

Sidebar

Related Questions

I think I've figured out the problem. I'm using a IP webcam stream, doing
Think about the games like Spiral Knights and Minecraft(Which I believe both use LWJGL)
Think of the following services on one box: SOCKS proxy HTTP proxy SSH service
Think its a simple one. I am working on a site. The design is
i think this is an easy one for you: I have a Controller in
think this should be an easy one... I want to get the currently selected
I think this is an easy one. I have 2 Pictures/JPGs and i want
Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
Think my problem is I am trying to sum a count in the same

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.