The require of a Module in Node
- CommonJs
CommonJs treats each file as a module, and variables defined inside the module are private and cannot be used by other modules unless they are exposed using predefined methods (via the export and require keywords). CommonJs is best known for its implementation of Node.js.commonjs. One notable feature of CommonJs is that modules are loaded synchronously, which currently does not work with JavaScript in browsers due to broadband speed limitations.
- AMD
AMD is an Asynchronous Module Definition. It loads modules asynchronously without affecting the execution of subsequent statements. The code that depends on this module is defined in a callback function that will not run until the load is complete. RequireJs, currently popular on the front end, is an implementation of the AMD specification
Require and how it works
- We’ve already mentioned that Node follows the CommonJs specification, which uses the require keyword to load modules. Here’s a simple example:
Define a simple module
// person.js var person = { talk: function () { console.log("I am talking..." ); }, listren: funciton () { console.log("I am listrening..." ); } // More Function ... } module.exports = person;Copy the code
This implements a custom module that provides an interface and then uses module.exports to expose that interface for external use. External code that wants to use the methods in Person.js needs to import that interface using the require keyword.
// index.js
var person = require("./person.js");
person.talk();
Copy the code
** Note :** omits the relative path [./] when introducing custom modules, which results in errors.
Require’s cache policy
- Node automatically caches files imported through require, so that the next import does not need to go through the file system and reads directly from the cache. This cache is based on file path location, which means that even if there are two identical files, but they are on different paths, two copies will be maintained in the cache.
The require of the hidden trouble
- When require is called to load a module, the code inside the module is called, which can sometimes cause hidden bugs. For example:
// module.js
function test () {
setInterval(function () {
console.log("test");
},1000)
}
test();
module.exports = test;
// run.js
var test = require("./module.js");
Copy the code
- Run.js does nothing except load a module. If you try running it, you will find that the test string is printed every second, and the run.js process does not exit.
- Loading a module points to code inside the module. In module.js, a constant timer is set so that run.js will always run.
- Above is an extreme example, camera a scene, when you call someone else has written module, clearly all the call has ended, but the caller process can’t quit anyway, this is likely to be have a hidden inside the called module loop or a database connection has been opened, This problem may not be noticed or triggered during development, and can result in a serious memory leak if it is actually in production.
Modularity and scope
- Now that modularity is mentioned, let’s talk about scope, focusing on this
- There are some differences between Node and this in JavaScript, and the Node console and script files have different policies. For JavaScript in a browser, this points to and behaves the same whether in a script or a console. Node does not
- This in the console is global first, running in Node Repl and Chrome console respectively:
console.log(this); // Chrome prints window // Node prints global objectCopy the code
As you can see, in the Node Repl environment, the global this refers to the global object. Continue with the following code:
var a = 10; console.log(this.a); // Chrome output 10; // Node Repl prints 10Copy the code
In the Node console, global variables are mounted under Global.
- This in the script
Let’s create a new file named test.js and add the following code to the file:
console.log(this);
Copy the code
Running Node test.js prints out an empty object. Then the following code:
var a = 10;
console.log(this.a); // undefined
console.log(global.a); // undefined
Copy the code
The first line of code defines a variable that is not mounted on the global this or global object. However, if you declare a variable without using the var or let keyword, as in the following code:
a = 10; console.log(global.a); / / 10Copy the code
It can print the results normally. What does the global this defined in the Node script file refer to? Module. Exports.
this.a = 10; console.log(module.exports); / / 10Copy the code
- To summarize, the global this and Global of the console can be treated as the same object in a Node repl environment, whereas they are not equivalent in a script file.
Type of scope in Node
- Global scope
- If a variable is not decorated with keywords such as var, let, or const, it belongs to the global scope. Variables defined in the global scope can be accessed through the global object. For example:
a = 10l console.log(global.a); / / 10Copy the code
- Module scope
-
Variables decorated with var, let, or const at the top level of the code file (not in any method or object) are in the module scope. The scope between different module scopes is isolated.
-
The module scope employee’s this points to module. Exports, for example, as mentioned earlier:
this.a = 10; console.log(module.exports); / / 10Copy the code