# nodejs - module system

How the module system, CommonJS & require works

Node.js v6.11.2 Documentation - Modules

Node.js module system is based on the CommonJS spec.

ES6 modules, Node.js and the Michael Jackson Solution

  • CommonJS to the rescue
  • How ES6 modules work and why Node.js hasn’t implemented it yet
  • A note about Babel
  • Why ES6 modules are good and how to get the best of both worlds

# articles

NodeJS require('..')? : stackoverflow.com

Requiring modules in Node.js: Everything you need to know : medium.freecodecamp.org : 20170319

# tips & rules

# module wrapper

Variables inside a module are private to that module because each module is wrapped.

(function(exports, require, module, __filename, __dirname) {
// Your module code actually lives in here
});
1
2
3

See module wrapper doc section for details.

The CommonJS module system uses a runtime evaluation of the modules, wrapping them into a function before the execution.

# modules are singleton

Module caching in the node documentation.

Everytime you use require('awesome-module'), node returns the same instance. If you want to execute initialization code at each require call, exports a function.

Module unicity is based on the directory (see Module Caching Caveats).

# module types

The module Object definition.

# core module

The node builtin modules (http, assert, ...).

They have priority over others modules. For example if a file module is http.js, require('http'); will result loading the node http module and not the file.

# file module

File extension is optional.

A required module prefixed with '/' is an absolute path to the file. For example, require('/home/marco/foo.js') will load the file at /home/marco/foo.js.

A required module prefixed with './' is relative to the file calling require().

Without a leading '/', './', or '../' to indicate a file, the module must either be a core module or is loaded from a node_modules folder.

# folder module

There are three ways in which a folder may be passed to require() as an argument.

# folder contains a default index.js

If there is no package.json file present in the directory, then node will attempt to load an index.js out of that directory.

For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load ./some-library/index.js.

# folder is defined with a package.json

Create a package.json file in the root of the folder, which specifies a main module.

{"name":"some-library", "main":"./lib/some-library.js"}
1

If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.

# folder is in a node_modules directory

If node cannot load a default index.js file nor find a package.json, it will search for node_modules directory on the file system.

See Loading from node_modules Folders.