Detailed Explanation of Module Definition Instance in nodejs

  • 2021-08-05 08:18:06
  • OfStack

This paper describes the module definition method in nodejs with examples. Share it for your reference, as follows:

1. Module definition

nodejs so-called module is a file! A. js file is a module of nodejs, and the module corresponds to file 11, so the reference module is require ('File Path').

Such as:


var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
      + circle.area(4));

This one is named foo. js


var PI = Math.PI;
exports.area = function (r) {
 return PI * r * r;
};
exports.circumference = function (r) {
 return 2 * PI * r;
};

This one is named circle. js

Two js code files are placed in the same folder.

If you want to reference modules in other folders, write the path directly. The path format here is the same as linux's path format 1:/.../.... js this is the absolute path,../. js this is the upper layer./. js this is the current path.

Note the difference between exports and module. exports: exports is actually only a reference to module. exports (like a = 2, b = a, b only refers to a, when b = c, b does not point to a), so you can understand the meaning of official website (if you want to declare the root of exports as a function, or you want to generate an object, please use ES50exports instead of exports)

2. Circular references

a.js:


console.log('a starting');
exports.done = false;
var b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');

b.js:


console.log('b starting');
exports.done = false;
var a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');

main.js:


console.log('main starting');
var a = require('./a.js');
var b = require('./b.js');
console.log('in main, a.done=%j, b.done=%j', a.done, b.done);

See, this a. js and b. js are referenced to each other. Will this be an infinite cycle? No, it's just that one is not fully loaded, that is, one module1 part is unavailable. As here, a. js is loaded first, but while a. js is loaded, a. js loads b. js. At this time, a. js is in a stagnant state, only the data before require is loaded, and b. js will be loaded straight. The following is the result of the run:


$ node main.js
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done=true, b.done=true

3. Core modules

The so-called core module is actually the public package developed by nodejs, just like the public package 1 of Java. To access the core module, only require ('file name ') is needed, so that it can be accessed. In fact, the public module package is placed under node_modules\ npm\ lib installed by nodejs.

4. Document module

When the so-called reference is not exactly matched, nodejs will first adopt the extensions:. js,. json, and then. node. The js file is the JavaScript file,. json will be parsed in json format, and. node will be loaded as an additional module as dlopen

Also note here that the module is loaded in node_modules without the notation of '/', './'. See below for where this file is.

5. node_modules folder

If a module is referenced without the formatting notation '/' '../' './', then the way to look for it is as follows: Suppose your file is in '/home/ry/projects/foo. js', which refers to require('bar.js') Then this bar. js is found as follows:

/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js

That is, from the current roadbed, add the node_modules folder to the parent directory step by step as the module address.

require('example-module/path/to/file') This reference and require('bar.js') It is the analysis of one kind.

6. Reference module by folder name

There are roughly two types: 1) Write the package. json file, which is written in the root directory of the project and has the following form:


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

What require ('./some-library') is really


require('./some-library/lib/some-library.js')

2) Directly agreed to load the index. js or index. node file, as in require above, which might be loaded as follows:

./some-library/index.js
./some-library/index.node

7. Caching

Multiple references to a module will only be loaded once. Just like the static keyword modification in java. But it's worth noting that when you require('foo') This does not guarantee that the same file will be referenced every time (because you may refer to it in different folders).

I hope this paper is helpful to everyone's nodejs programming.


Related articles: