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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:51:59+00:00 2026-06-13T14:51:59+00:00

Is it possible to pass arguments when loading a module using require? I have

  • 0

Is it possible to pass arguments when loading a module using require?

I have module, login.js which provides login functionality. It requires a database connection, and I want the same database connection to be used in all my modules. Now I export a function login.setDatabase(…) which lets me specify a database connection, and that works just fine. But I would rather pass the database and any other requirements when I load the module.

var db = ...
var login = require("./login.js")(db);

I am pretty new with NodeJS and usually develop using Java and the Spring Framework, so yes… this is a constructor injection 🙂 Is it possible to do something like the code I provided above?

  • 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-13T14:52:00+00:00Added an answer on June 13, 2026 at 2:52 pm

    Based on your comments in this answer, I do what you’re trying to do like this:

    module.exports = function (app, db) {
        var module = {};
    
        module.auth = function (req, res) {
            // This will be available 'outside'.
            // Authy stuff that can be used outside...
        };
    
        // Other stuff...
        module.pickle = function(cucumber, herbs, vinegar) {
            // This will be available 'outside'.
            // Pickling stuff...
        };
    
        function jarThemPickles(pickle, jar) {
            // This will be NOT available 'outside'.
            // Pickling stuff...
    
            return pickleJar;
        };
    
        return module;
    };
    

    I structure pretty much all my modules like that. Seems to work well for me.

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

Sidebar

Related Questions

When queuing a new build using VS it is possible to pass in arguments
Is it possible to pass arguments to my library, which is loaded with LD_PRELOAD
Is it possible to pass arguments to an Android Handler?? I have two pieces
Possible Duplicate: pass constructor arguments using PDO::FETCH_CLASSTYPE How to put arguments in the fetch
Is it possible to pass additional arguments to an event callback? For example, if
Possible Duplicate: pass data from Action To Another Action I have a view in
Is it possible to pass *args to string.format? I have the following function: @classmethod
I was wondering if in C/C++ language it is possible to pass arguments to
Assuming it's possible, how would one pass arguments by reference to a variadic function
Is it possible a lambda function to have variable number of arguments? For example,

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.