Skip to content

03. Module parameters

poseidonCore edited this page May 8, 2016 · 9 revisions

######INITIALISATION PARAMETERS Many browser-side requirers provide an environment with only require, module, exports.

However, for many Node.js modules to work, they also require access to __filename and __dirname.

NodalRequire will resolve the full address and then put the file name in __filename and the path leading up to the file name in __dirname. Some points to note about the canonical forms:

  • __filename includes the .js extension (but does not include an search tag).
  • __dirname does not include a final /.
  • These are all lower case. Unlike node.js, currently different modules of different casing resolve to the same module. This is because the web is largely case-insensitive and building in case sensitivity at this stage may make things harder rather than easier, but this may change later if different registry differentiation is required.
  • module.address = __filename currently, but this may change later if different aliasing is required.

######MODULE PROPERTIES
The module object contains the following properties:

  • __dirname:String The registered address of the folder containing the module's (lower case).
  • __filename:String Alias for address.
  • address:String The registered address of this module (lower case).
  • definition:String The bare text of the module's definition, without the surrounding functional decoration that is added to form the constructor.
  • error?:Error Any error loading the module.
  • exports:Object The exports of the module. This is used for 'this' in binding.
  • constructor:ModuleConstructor A function used to form the module closure: constructor = new Function(..., definition).
  • isInitialised:Boolean When the all of module's requirements are loaded, it can be initialised upon first call.
  • type:"node"|"local"|"dynamic" "node": sourced from a node_module folder along the caller's lineage; "local": sourced via a file path ('./'. '../', 'http://', etc); "dynamic": defined dynamically.

These are likely to increase if required.

They can be accessed directly from within a module via module[].


######INCOMPATIBILITIES WITH NODE.JS MODULE PROPERTIES
A Node.js module has the following interface:

interface NodeModule {
    exports:Object;
    require:(id:String)=>any;
    id:String;
    filename:String;
    loaded:Boolean;
    parent:Object;
    children:Object[];
}

TODO: Currently, these inconsistencies may be a source of difficulty for some modules. They are planned to be properly abstracted.

Clone this wiki locally