Skip to content
Jim Amsden edited this page Feb 21, 2018 · 3 revisions

Node modules are defined in a hierarchy of folders containing node_modules folder. A node_modules folder contains:

  • package.json - describes the module
  • usually index.js - does the requires for all the module parts and is invoked automatically by the Node when it looks up a module
  • Module lookup starts in the current directory, then goes to node_modules, then works up the directory structure looking in any other node_modules subfolders until it looks in the global node installation.
  • modules export things that are intended to be used by the user of the module - either a function or function class.

Module.exports is returned from require(). This can be set to a function class, or different parts of the module can be set as properties of module.exports in order to access more than one exported item - require(‘aModule’).someExportedThing.

Modules can be required more than once in different modules in the same node process, but only the first one executes the module to initialize its contents (exports) - the rest return references to already initialized contents.

Clone this wiki locally