CommonJs (NodeJs) - Require

About

The import functionality implementation of commonjs (and then node) is done via the require function

require basically:

It is the built-in statement from the commonjs module format to load module.

Require vs Import

require permits to load CommonJs Module whereas the import statement permits to load ESM module

Node.js follows by default the commonJs format, if you want to use import, you need to:

Syntax and High-level algorithm of require in Node

If the code is run directly on Node without any other tool, the require node function is used to resolve (find) the dependency.

The below require statement called from a module located at path Y

require(X) 

where:

Require will resolve the module location with the following algo:

  • 1. If X is a core module,
  • 2. If X begins with /
    • a. set Y to be the filesystem root
  • 3. If X begins with ./ or / or ../, it indicates:
  • 4. otherwise try to load from:
  • 5. THROW “not found”

More .. see high-level algorithm in pseudocode of what require.resolve() does

Dependency Cycle

See https://nodejs.org/api/modules.html#modules_cycles

Module File Extension

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

  • .js (interpreted as JavaScript text files)
  • .json (parsed as JSON text files)
  • and finally .node. (interpreted as compiled addon modules loaded with dlopen)

Example

Node Module

Local Package installed with Javascript - npm (Node package manager)

var demo=require('package-name');

demo.printMsg

Project Module

const { PI } = Math;

exports.area = (r) => PI * r ** 2;

exports.circumference = (r) => 2 * PI * r;
  • The client of the modules> By giving the path, node know that it's a project module
//  loads the module circle.js that is in the same directory as foo.js.
const circle = require('./circle.js');
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);





Discover More
Browser
Browser - Userscript (user.js)

userscript are javascript script that are stored on the browser side and that can run: * via an * or via the devtool * : ViolentMonkey * : Greasemonkey *...
Welcome From Browser
How to develop, publish and use a javascript library ?

A step by step tutorial on how to create and publish a javascript library
How to resolve the SyntaxError: Cannot use import statement outside a module?

This article shows you how to resolve the syntax Error: Cannot use import statement outside a module.
Javascript - CommonJs

CommonJs is a Module format definition They have defined: * a Module * and packages definition The CommonJS...
Javascript - Module Loader (Script Loader)

A loader searches and loads module and script into the javascript engine (environment). From the main script, they will: create a dependency graph from the require and import statement find them...
Javascript - Transpiler (Transpiling)

A transpiler permits to take an language source and to transform it in a language target. The language source and target may be the same but with two differents version. compiling A transpiler permits...
Javascript - npm (Node package manager)

Npm is the package manager that is packaged/installed with a node installation. Automatically, with a node installation. Manually: -{VERSION}.tgz To update...
Javascript ES - Module Script (esm / .mjs)

The module implementation in Ecma Script (ES2015). An ES6 module is a Javascript file: automatically set in strict-mode with export statement and / or statement Everything inside a module is...
Javascript Module - Dependency Graph

This page is the dependency graph between module. The dependency graph is build from a single module (called the entry point) for a ESM module with the import statement for a commonJs module (node...
Javascript Module - Import (Es Module)

This page is the import statement defined in the es module specification The ES6 import is: a function for dynamic import (performed at runtime time) or a statement for static import (performed...



Share this page:
Follow us:
Task Runner