I am often told that blocking of any type is a no-no in node.js, and that asynchrony is one of its main imperatives. You can try the following.
A quote from a non-blocking response is required in node.js
Here's how it is implemented:
> console.log(require.extensions['.js'].toString())
function (module, filename) {
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
module._compile(stripBOM(content), filename);
}
You can do the same in your application. I think something like this will work:
var fs = require('fs')
require.async = function(filename, callback) {
fs.readFile(filename, 'utf8', function(err, content) {
if (err) return callback(err)
module._compile(content, filename)
callback(null, require(filename))
})
}
require.async('./test.js', function(err, module) {
console.log(module)
})
source
share