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

  • Home
  • SEARCH
  • 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 8444323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:22:43+00:00 2026-06-10T09:22:43+00:00

Say I create a library in ./libname which contains one main file: main.js and

  • 0

Say I create a library in ./libname which contains one main file: main.js and multiple optional library files which are occasionally used with the main object: a.js and b.js.

I create index.js file with the following:

exports.MainClass = require('main.js').MainClass; // shortcut
exports.a = require('a');
exports.b = require('b');

And now I can use the library as follows:

var lib = require('./libname');
lib.MainClass;
lib.a.Something; // Here I need the optional utility object
lib.b.SomeOtherThing;

However, that means, I load ‘a.js’ and ‘b.js’ always and not when I really need them.

Sure I can manually load the optional modules with require('./libname/a.js'), but then I lose the pretty lib.a dot-notation 🙂

Is there a way to implement on-demand loading with some kind of index file? Maybe, some package.json magic can play here well?

  • 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-10T09:22:45+00:00Added an answer on June 10, 2026 at 9:22 am

    Looks like the only way is to use getters. In short, like this:

    exports = {
        MainClass : require('main.js').MainClass,
        get a(){ return require('./a.js'); },
        get b(){ return require('./a.js'); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I create one object and add it to my ArrayList . If I
lets say I have a library object (node1) which i have instantiated twice (mc1
When I create my own non-standard path for library files (say, ~/lib) I can
Let's say there is a library function called get_pack() which returns a Pack object:
Which dll or library can be used to create the excelsheet which is not
I would like to create a library, say foolib, but to keep different subpackages
Say I create some object class like so public class thing { private String
Let us say that I am accessing a third-party library, for which the documentation
How can a standard-library module (say math) be accessed when a file prog.py is
I am trying to create a parent WPF/surface application which will host multiple WPF/surface

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.