require

134 views
Skip to first unread message

Angelo Chen

unread,
Nov 15, 2012, 7:17:32 AM11/15/12
to nod...@googlegroups.com
Hi,

var fs = require('fs');
I'd like to understand this a little more, require is part of v8 JS? or nodejs function? if yes, where can I find it in Nodejs's source code? thanks,

Angelo

Angelo Chen

unread,
Nov 15, 2012, 9:20:45 AM11/15/12
to nodejs
Hi,

Thanks for the direction, but I found it at beg of module.js, it has a
line:
var NativeModule = require('native_module');
is the require here refers to:

Module.prototype.require = function(path) {
return Module._load(path, this);
};
?

On Nov 15, 8:21 pm, Вадим Барышев <vadimbarys...@gmail.com> wrote:
> This is node.js function. You can start
> herehttps://github.com/joyent/node/blob/master/lib/module.js#L361
>
> четверг, 15 ноября 2012 г., 16:17:32 UTC+4 пользователь Angelo Chen написал:

Вадим Барышев

unread,
Nov 15, 2012, 9:39:33 AM11/15/12
to nod...@googlegroups.com
NativeModule defined here https://github.com/joyent/node/blob/master/src/node.js#L676

It just returns itself if module id is 'native_module' https://github.com/joyent/node/blob/master/src/node.js#L687

Module._load defined here https://github.com/joyent/node/blob/master/lib/module.js#L275

четверг, 15 ноября 2012 г., 18:21:09 UTC+4 пользователь Angelo Chen написал:

Martin Cooper

unread,
Nov 15, 2012, 11:28:37 AM11/15/12
to nod...@googlegroups.com
On Thu, Nov 15, 2012 at 6:20 AM, Angelo Chen <angelo...@gmail.com> wrote:
Hi,

Thanks for the direction, but I found it at beg of module.js, it has a
line:
var NativeModule = require('native_module');
is the require here refers to:

Module.prototype.require = function(path) {
  return Module._load(path, this);
};
?

I'm afraid you're going to have to keep reading. :-) You'll discover that ultimately 'require' is a function that is unique for each module, and is passed in to the wrapper that executes the module. See:

 
NativeModule.require() is used to get at those modules that are baked into Node itself; a regular 'require' is used for everything else.

--
Martin Cooper


On Nov 15, 8:21 pm, Вадим Барышев <vadimbarys...@gmail.com> wrote:
> This is node.js function. You can start
> herehttps://github.com/joyent/node/blob/master/lib/module.js#L361
>
> четверг, 15 ноября 2012 г., 16:17:32 UTC+4 пользователь Angelo Chen написал:
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > var fs = require('fs');
> > I'd like to understand this a little more, require is part of v8 JS? or
> > nodejs function? if yes, where can I find it in Nodejs's source code?
> > thanks,
>
> > Angelo

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: 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 post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply all
Reply to author
Forward
0 new messages