38ac4c17719e5f313bf4b0709d5a8518027484b1
[platform/upstream/iotjs.git] / tools / docs / api / IoT.js-API-Module.md
1 ## Module
2 ``` module ``` implements ``` require ``` function.
3 Since ``` require ``` function is implicitly passed to user code, user do not need to import it explicitly.
4
5 ### Methods
6
7 #### require(id)
8 * ``` id : String ``` - module name to be loaded
9
10 Loads the module named 'id'
11
12
13 ### Module Loading by ``` require ``` function
14
15 #### ``` require ``` search paths
16 ``` require ``` function finds modules in the order of
17
18 1. current working directory
19 2. node_modules directory under current working directory
20 3. $HOME/node_modules
21 4. $NODE_PATH/node_modules
22
23
24 #### ``` require('id') ``` works as follows:
25 For each directory in search paths above,
26
27 0. If native module named 'id' exists, load it and return.
28 1. If id.js file exists, load it and return.
29 2. If a directory id exists, load it as an package and return.
30
31 #### ``` require('id') ``` as a package
32 For a given id, if a directory id exists, module system consider the directory as a package.
33
34 0. if 'id/node_modules/package.json' contains 'main' property, load the file named main property.
35 1. if either id/node_modules/package.json or main property exists, load index.js