modules

The CommonJS module specification was originally the standard used in Node.js for working with modules. Most of the modules you can install with npm are built on the CommonJS format.

Since the ES6 modules specification is standard for JavaScript, Node.js fully supports it and provides limited interoperability between them and the existing module format, CommonJS.

Unlike ES6 modules, CommonJS modules are only loaded synchronously and were designed to be used on the server-side. However, you can write code to load modules asynchronously and convert the module for use in a browser.

module scope in CommonJs

For every module following names are accessible.

name description
__dirname Variable, that contains the directory name of the current module.
__filename Variable, that contains the file name of the current module.
module A reference to the current module object.
exports A reference to the module.exports.
requre(id)

Used to import modules, JSON, and local files.

The id parameter is module name or path to the module.

export in CommonJs

You should use built-in module.exports object to specify exported names.

It is recommended to specify export names at the end of the module.

class ClsDemo {}
function myfunc(arg){}
var myvar="";
var myfuncexpr = (e) => {};
const pi = 3.14;

 
module.exports = {
    ClsDemo,
    myfunc,
    myvar,
    myfuncexpr,
    pi
}

import in CommonJs

The reqire(id) is used to import modules, JSON and local files, where the id parameter is module name (see folder as module) or path to the module file.

const path = require('path'); // import build-in module
const log = require('fancy-log'); // import installed module
const foo = require('./foo.js'); // import module foo.js from current folder
...

// using imported module
for (var result of matches) {
    const newEntry = path.resolve(path.dirname(entry), result[1]);
    ...
}    

If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: .js, .json, and finally .node.

Local modules and JSON files can be imported using a relative path (e.g. ./, ./foo, ./bar/baz, ../foo) that will be resolved against the directory named by __dirname (if defined) or the current working directory.

If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node.js will not append node_modules to a path already ending in node_modules. If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.

folder as module

It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to those directories.

In this case index.js or index.node file is expected as entry point.

Also you can specify entry point in package.json file in your folder. For example, it can look like this

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