On Friday, 6 November 2015 08:39:12 UTC+11, Ingwie Phoenix wrote:
>
> Hey folks.
>
> While developing a function that lets me require() custom files, I took
> the original JS and jSON "loaders" as examples.
>
> But what I saw within the JS version confused me: It didn’T return a
> thing!
>
>
> > console.log(require.extensions[".js"].toString())
> function (module, filename) {
> var content = fs.readFileSync(filename, 'utf8');
> module._compile(internalModule.stripBOM(content), filename);
> }
>
>
> So - is require() "really" sync? Or, is it actually async?
>
> Kind regards, Ingwie!
It is sync, but you are not returning anything. Result of _compile is in
module.exports.
This is the code I use to require manually (in my case I didn't want it to
be cached by module system):
function myRequire(filename) {
var m = new module.constructor();
m.paths = module.paths;
var src = fs.readFileSync(filename).toString();
m._compile(src, filename);
return m.exports;
}
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/330edfd2-4366-4003-8c66-310169b5cd08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.