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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:16:52+00:00 2026-06-16T04:16:52+00:00

I would like to have the ability to load a module that has been

  • 0

I would like to have the ability to load a module that has been changed. I would have to of course unload the module first. Since this is a webserver setup, I am asking if there is a way to load the module in an async fashion, to avoid freezing the webserver for the duration of the read of the updated file.

Awhile back Node.JS removed the require.async function. So, on the latest version of Node.JS, what would be the recommended alternative?

  • Should I read the entire file first, and then use the Module library to parse the file contents. (as opposed to the default functionality of taking a file name to read and parse internally) How?
  • Should I outsource this job to some open-source library? Which one?
  • Should I write my own module handler – my own implementation of requireAsync? (I know how.)

Note: I do not want to do anything more than load a module async, so please do not recommend that I replace my setup with a new webserver routing framework.

  • 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-16T04:16:54+00:00Added an answer on June 16, 2026 at 4:16 am

    I posted this answer, but you are welcome to post an improvement.

    See the Node.JS source code.

    Module.prototype.require = function(path) { return Module._load(path, this); };

    Abridged version of _load

    Module._load = function(request, parent, isMain) {
      var filename = Module._resolveFilename(request, parent);
      if (Module._cache[filename]) return Module._cache[filename].exports;
      if (NativeModule.exists(filename)) {
        if (filename == 'repl') { // special case, needs the real require.
          var replModule = new Module('repl');
          replModule._compile(NativeModule.getSource('repl'), 'repl.js');
          NativeModule._cache.repl = replModule;
          return replModule.exports;
        }
        return NativeModule.require(filename);
      }
      var module = new Module(filename, parent);
      if (isMain) process.mainModule = module, module.id = '.';
      Module._cache[filename] = module;
      var hadException = true;
      try {
        module.load(filename);
        hadException = false;
      } finally {
        if (hadException) delete Module._cache[filename];
      }
      return module.exports;
    };
    

    My version of require.async.js will be something like

    var NativeModule = require('native_module');
    var fs = require('fs');
    if(!require.async) require.async = function (path, callback) { module.exports(path, this, callback); } // Comment out if you dislike using globals
    module.exports = function(request, parent, callback) {
      var filename = Module.resolve(request, parent); // This is a Sync function. TODO, change it to an async function with a callback.
      if (Module.cache[filename]) callback(Module.cache[filename].exports);
      else if (NativeModule.exists(filename)) callback(new Error('What are you thinking?'))
      else fs.readFile(filename, 'utf8', function(err, file) {
        if (Module.cache[filename]) callback(null, Module.cache[filename].exports); // For the case when there are two calls to require.async at a time.
        else if(err) callback(err)
        else {
          var module = new Module(filename, parent);
          try {
            module._compile(file);
            Module.cache[filename] = module;
          } catch(ex) {
            callback(err)
          }
          if(Module.cache[filename]) callback(null, module.exports)
        }
    }
    

    Caveats

    • There is one TODO in the code which is to make the multiple calls to stat to be async. The actual reading of the file is regular async, so that is good.
    • If you are async loading a module, and that module you are loading is sync loading another module, then you have not fully gone async with your code – have you.
    • It uses one private method – _compile.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:
In php, I would like to have the ability to re-order an associative array
Is Delphi have any ability to write script of IDE actions? I would like
I have a sample dataform and I would like to remove the ability to
I would like to have a border that is 4px thick pink on the
I would like to have the ability to process Win32 messages in a console
I do have my own Controller in Magento, that has been done by following
I'm working on a photo gallery, and I would like the ability to have
I would like to have one view called Example and have the ability to

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.