Briefly master the specifications and usage of CommonJS

Preface: The starting point of the CommonJs specification: the background JS has no module system, less standard libraries, and lack of package management tools; in order to allow JS to run anywhere, to achieve the ability of Java, C#, PHP, and other background languages ​​to develop large-scale applications; the fact The module specification has already appeared in ES6. If the module specification of ES6 is used, the node.js environment is not required. Therefore, a distinction needs to be made between the commonJS specification and the ES6 module specification.
1. The CommonJS module specification is mainly divided into three parts: module definition, module identification, and module reference.
a. A file is a module with a separate scope;
b. Variables, functions, and objects defined in ordinary ways belong to the module;
c. The module is loaded through require;
d. The module is exposed through exports and modul.exports the content of;

Module definition, module identification:

//在数据库模块database.js文件中,把各个函数写好,然后暴露出来
 module.exports={
    'saveDatabase':saveDatabase,//保存数据函数
    'saveLastSpider':saveLastSpider,//保存最后一条数据函数
    'getInfoByType':getInfoByType,//通过类型查找函数
    'getInfoByOrder':getInfoByOrder,//通过排序查找函数
 }
//函数具体的实现没有贴出来,上面名称的是函数名。

Module reference:

//在控制模块controller.js中引用上面定义好的模块
var database = require('./database');//引用模块,将生成的对象指向database对象(命名可以是任意定的)
database.saveDatabase([1,2,3,4,5]);//通过database对象调用相应的函数

3. All code runs in the module scope and will not pollute the global scope; the module can be loaded multiple times, but it will only run once when it is loaded for the first time, and then the running result will be cached. Directly read the cached result; the loading order of modules is synchronously loaded according to the order of appearance of the code;

4. __dirname represents the folder path where the current module file is located, and __filename represents the folder path + file name where the current module file is located;

5. The basic function of require (synchronous loading): read and execute a JS file, and then return the exports object of the module. If the specified module is not found, an error will be reported;

6. Exports in the module: For convenience, node provides an exports variable for each module, which points to module.exports, which is equivalent to adding this sentence to the module header: var exports = module.exports, when exporting externally, You can add methods to the exports object, PS: you cannot assign values ​​directly (because this cuts off the connection between exports and module.exports);

7. npm root -g: View the npm global package installation location, it is recommended to create a new npm\node_modules directory in the nvm directory, and then set the npm global package installation location: npm config set prefix "", and then add the path to the environment variable ;

8. npm init -y: initialize a package.json file, and adding -y will generate the file by default, no need to fill in step by step; npm docs package name: view the package documentation; npm install: install the dependencies property in package.json All dependent packages in

9. Since the server of npm is abroad, if you do not have the harmony tool, you cannot download it. It is recommended to use the Taobao NPM mirror: http://npm.taobao.org/ , the synchronization frequency with the official NPM is currently once every 10 minutes ;Installation command: npm install -g cnpm --registry= https://registry.npm.taobao.org , installation package: cnpm install package name (other commands are basically the same);

10. If you do not want to download cnpm, npm also provides a mirror source management tool: npm install -g nrm, through: nrm ls, view the mirror source list, and switch through: npm use mirror source;

11. NPM's module loading mechanism:

  如果require的是绝对路径文件,查找不会去遍历每个node_modules目录,其速度最快

  1). Take the first directory from the module.paths array (from the current execution file directory to the disk root directory) as the search reference

  2). Find the file directly from the directory, if it exists, end the search, if not, go to the next search

  3). Try to search after adding .js, .node, .json suffix, if there is a file, end the search, if not, proceed to the next search

  4). Try to find the parameter of require as a package, read the package.json file in the directory, and get the file specified by the Main parameter

  5). Try to find the file, if it exists, end the search, if it does not exist, perform the third search

  6). If it continues to fail, take out the next directory in the module.paths array as a benchmark search, and loop steps 1-5

  7). If it continues to fail, loop steps 1-6 until the last value in module.paths

  8). If it continues to fail, throw an exception

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325376197&siteId=291194637