'require' method

84 views
Skip to first unread message

Eric Marshall

unread,
Aug 23, 2014, 12:09:51 AM8/23/14
to nod...@googlegroups.com
Hi All,

I looked up the source code of Node.js to get more details about 'require'. And found this is the function definition of 'require': 

function require (moduleName) {} 

The function body is empty. How can this function load a module?

Thank you for your help!
Eric

Ryan Schmidt

unread,
Aug 25, 2014, 2:33:57 AM8/25/14
to nod...@googlegroups.com

On Aug 22, 2014, at 11:09 PM, Eric Marshall wrote:

> I looked up the source code of Node.js to get more details about 'require'. And found this is the function definition of 'require':
>
> function require (moduleName) {}
>
> The function body is empty. How can this function load a module?

In what file did you see that definition?


Andrey

unread,
Aug 26, 2014, 12:17:58 AM8/26/14
to nod...@googlegroups.com
Hi Eric!

Usually "require" is a reference to this function: https://github.com/joyent/node/blob/a5778cdf01425ae39cea80b62f9ec6740aec724a/lib/module.js#L362
It's passed to your script as wrapper function parameter when you require it - https://github.com/joyent/node/blob/a5778cdf01425ae39cea80b62f9ec6740aec724a/lib/module.js#L459

Inside module.js itself, "require" is a reference to a much simpler function NativeModule.require - https://github.com/joyent/node/blob/a5778cdf01425ae39cea80b62f9ec6740aec724a/src/node.js#L752
which is only able to load modules from process.binding('natives') hash (it's populated with content of ./lib folder by this action https://github.com/joyent/node/blob/a5778cdf01425ae39cea80b62f9ec6740aec724a/node.gyp#L414 and this script: https://github.com/joyent/node/blob/a5778cdf01425ae39cea80b62f9ec6740aec724a/tools/js2c.py )

Cheers,
Andrey
Reply all
Reply to author
Forward
0 new messages