1, overview
COMMONJS is a specification for server-side modules, and node. JS uses this specification.
According to the COMMONJS specification, a single file is a module. The load module uses the Require method, which reads a file and executes it, and finally returns the exports object inside the file. The following is a simple module file example.js.
Console.log ("Evaluating Example.js"); var function () { console.log ("invisible"= "HI"function () { Console.log ( message);}
Using the Require method, load the example.js.
var example = require ('./example.js ');
At this point, the variable example corresponds to the exports object in the module, so it is possible to use the various methods provided by the module through this variable.
{ "hi", say: [Function]}
The JS file name needs to be preceded by a path, which can be a relative path (as opposed to a file using the Require method) or an absolute path. If you omit the path, node. JS will assume that you want to load a core module, or a module that is already installed in the local node_modules directory. If a directory is loaded, node. JS will first look for the Package.json file in that directory, load the module referenced by the main property of the file, or look for the Index.js file under that directory.
The following example uses a single line of statements to define the simplest module.
// Addition.js exports. Do function return A + b};
The above statement defines an addition module by defining a Do method on the exports object, which is the method for external invocation. When used, simply call with the Require function.
var add = require ('./addition '); Add. Do (from) // 3
Let's look at a more complicated example.
// Foobar.js function Foobar () { thisfunction() { console.log (' Hello foo '); } This function () { console.log (' Hello bar '); = Foobar;
The method to invoke the module is as follows:
var foobar = require ('./foobar '). Foobar, test new// ' Hello Bar '
Sometimes, you don't need exports to return an object, only it returns a function. At this time, we should write Module.exports.
function () { console.log ("Hello World")}
compatibility of 2,AMD specifications with COMMONJS specifications
The COMMONJS specification loading module is synchronous, that is, only the loading is complete before the subsequent operation can be performed. The AMD specification is an asynchronous load module that allows you to specify a callback function. Since node. JS is primarily used for server programming, the module files are generally present in the local hard disk, so it is faster to load, regardless of the way asynchronous loading, so the COMMONJS specification is more applicable. However, if it is a browser environment, to load modules from the server side, you must adopt the asynchronous mode, so the browser side is generally used AMD specifications.
The AMD specification uses the Define method to define the module, and here is an example:
function (Lib) { function foo () { lib.log (' Hello world! ') ); } return { foo:foo };});
The AMD specification allows the output module to be compatible with the COMMONJS specification, at which point the Define method needs to be written as follows:
Define (function (require, exports, module) { var somemodule = require ("Somemodule" ); var anothermodule = require ("Anothermodule"); Somemodule.dotehawesome (); Anothermodule.domoarawesome (); function () { somemodule.dotehawesome (); Anothermodule.domoarawesome (); };});