Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js module system


May 10, 2021 Node.js


Table of contents


Node .js module system

In order for .js files that Node can call each other, node .js provides a simple module system.

Modules are an .js Ofe applications, and files and modules correspond one-to-one. In other words, a Node .js file is a module that might be JavaScript code, JSON, or a compiled C/C?extension.

Create a module

In node .js, it's easy to create a module, and let's create a 'main .js' file with the following code:

var hello = require('./hello');
hello.world();

In the above example, the code need ('./hello') introduces the hello.js file in the current directory (./ is the current directory, node.js the default suffix is js).

Node .js provides both exports and requires objects, where exports are the interface exposed by the module, and require is used to get the interface of a module from the outside, that is, the exports object of the acquired module.

Let's create a hello .js, coded as follows:

exports.world = function() {
  console.log('Hello World');
}

In the example above, hello.js uses the world as the module's access interface through the exports object, loads the module in main.js with require ('./hello'), and then directly accesses the member functions of the exports object in the main .js.

Sometimes we just want to encapsulate an object in a module in the following format:

module.exports = function() {
  // ...
}

For example:

//hello.js 
function Hello() { 
 var name; 
    this.setName = function(thyName) { 
       name = thyName; 
  }; 
   this.sayHello = function() { 
     console.log('Hello ' + name); 
  }; 
}; 
module.exports = Hello;

This allows you to get this object directly:

//main.js 
var Hello = require('./hello'); 
hello = new Hello(); 
hello.setName('BYVoid'); 
hello.sayHello(); 

The only change in the module interface is the use of module.exports and Hellos instead of exports.world , function(). W hen the module is referenced externally, its interface object is the Hello object itself, not the original exports.


Where are the modules on the service side

As you may have noticed, we've already used modules in our code. Like this:

var http = require("http");

...

http.createServer(...);

Node .js a module called "http" in our code, and we request it and assign the return value to a local variable.

This turns our local variable into an object with all the common methods provided by the http module.

The .js in node's require method is as follows:

Because there are 4 classes of modules (native modules and 3 file modules) in the Node .js, although the require method is extremely simple, the internal loading is very complex and has different loading priorities. Here's what it looks like:

Node .js module system

Loaded from the file module cache

Although native modules have different priorities than file modules, they do not take precedence over loading existing modules from the cache of file modules.

Loaded from the native module

The priority of the native module is second only to that of the file module cache. A fter the need method resolves the file name, it first checks whether the module is in the list of native modules. I n the case of the http module, for example, although there is an http/http.js/http.node/http.json file in the directory, the require ("http") is not loaded from these files, but from the native module.

Native modules also have a cache area, which is also preferred for loading from the cache. If the cache has not been loaded, the native module is called loaded and executed.

Loaded from a file

When the file module cache does not exist and is not a native module, Node.js parses the parameters passed in by the require method and loads the actual file from the file system, and the packaging and compilation details of the loading process are described in the previous section, where we will describe in detail the process of finding the file module, some of which are worth knowing.

The require method accepts the passing of the following parameters:

  • http, fs, path, etc., native modules.
  • ./mod or: /mod, a file module relative to the path.
  • /pathtomodule/mod, absolute path file module.
  • Mod, a file module that is not a native module.