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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:22:45+00:00 2026-06-03T18:22:45+00:00

maybe this question is a little silly, but is it possible to load multiple

  • 0

maybe this question is a little silly, but is it possible to load multiple .js files with one require statement? like this:

var mylib = require('./lib/mylibfiles');

and use:

mylib.foo(); //return "hello from one"

mylib.bar(): //return "hello from two"

And in the folder mylibfiles will have two files:

One.js

exports.foo= function(){return "hello from one";}

Two.js

exports.bar= function(){return "hello from two";}

I was thinking to put a package.json in the folder that say to load all the files, but I don’t know how. Other aproach that I was thinking is to have a index.js that exports everything again but I will be duplicating work.

Thanks!!

P.D: I’m working with nodejs v0.611 on a windows 7 machine

  • 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-03T18:22:48+00:00Added an answer on June 3, 2026 at 6:22 pm

    First of all using require does not duplicate anything. It loads the module and it caches it, so calling require again will get it from memory (thus you can modify module at fly without interacting with its source code – this is sometimes desirable, for example when you want to store db connection inside module).

    Also package.json does not load anything and does not interact with your app at all. It is only used for npm.

    Now you cannot require multiple modules at once. For example what will happen if both One.js and Two.js have defined function with the same name?? There are more problems.

    But what you can do, is to write additional file, say modules.js with the following content

    module.exports = {
       one : require('./one.js'),
       two : require('./two.js'),
       /* some other modules you want */
    }
    

    and then you can simply use

    var modules = require('./modules.js');
    modules.one.foo();
    modules.two.bar();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a little unconventional question but I'd like to hear an answer
Maybe this is a dumb question, but do event listeners use CPU cycles like
Maybe this is a silly question, but I'm working on a project that wants
This may be a silly question but... Say you have a sentence like: The
maybe this question is a little too general but maybe someone has experience the
Maybe this question is a little far-fetched but i'll give it a shot.. Can
Maybe my question is silly but there's one thing I wish to know about
maybe this question is a little basic, but I'm reading the official docs in
I find this question a little tricky. Maybe someone knows an approach to answer
Maybe this question has been asked many times before, but I never found a

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.