1

I'm trying to include modules in my nodejs application. The problem I'm facing is the code snippet below:

let adapter = new Adapter().then(function(result){
        let test= require('./loader');
        console.log(test);
}).catch(function(){});

Results always returned

{}

File loader.js

module.exports = {
   load_controller: () =>
   {
   }
};

Pls help me ^^

  • 1
    What do you expect that to do? Why? – SLaks May 21 '17 at 18:51
  • require modules are synchronous and it should work as written. try changing your code instead of focusing on require? see this http://stackoverflow.com/questions/20315434/node-js-asynchronous-module-loading – sbharti May 21 '17 at 19:08
  • Why you call `require` inside of `then`, and not before? – PRAISER May 21 '17 at 19:09

1 Answers1

0

require in node is synchronous.

Assuming you have a good reason to asynchronously load your module, this answer details how to do it. The tl;dr is to export a function that takes the callback of what you would like to do async.

Unless your code in ./loader actually depends on the new Adapter() being initialized first, then this isn't necessary.

Community
  • 1
  • 1
metame
  • 2,480
  • 1
  • 17
  • 22