Skip to content

Latest commit

 

History

History
35 lines (23 loc) · 1.1 KB

File metadata and controls

35 lines (23 loc) · 1.1 KB

Module

module implements require function. Since require function is implicitly passed to user code, user do not need to import it explicitly.

Methods

require(id)

  • id : String - module name to be loaded

Loads the module named 'id'

Module Loading by require function

require search paths

require function finds modules in the order of

  1. current working directory
  2. node_modules directory under current working directory
  3. $HOME/node_modules
  4. $NODE_PATH/node_modules

require('id') works as follows:

For each directory in search paths above,

  1. If native module named 'id' exists, load it and return.
  2. If id.js file exists, load it and return.
  3. If a directory id exists, load it as an package and return.

require('id') as a package

For a given id, if a directory id exists, module system consider the directory as a package.

  1. if 'id/node_modules/package.json' contains 'main' property, load the file named main property.
  2. if either id/node_modules/package.json or main property exists, load index.js