Is it possible to load a Node.js module asynchronously?
This is the standard code:
var foo = require(\"./foo.js\"); // waiting for I/O
foo.bar();
The npm module async-require can help you to do this.
npm install --save async-require
var asyncRequire = require('async-require');
// Load script myModule.js
asyncRequire('myModule').then(function (module) {
// module has been exported and can be used here
// ...
});
The module uses vm.runInNewContext(), a technique discussed in the accepted answer. It has bluebird as a dependency.
(This solution appeared in an earlier answer but that was deleted by review.)