採用了 Commonjs 規範,經過 module.exports、require
來導出和導入模塊。模塊加載機制中,採用了延遲加載的策略。就是說在用到的狀況下,系統模塊纔會被加載,等加載完成後會放到 binding_cache 中。javascript
http、buffer、fs
等,底層調用的內建模塊 (C/C++);express、koa、moment.js
等;.
、..
、/
開頭的;module.js
;module.json
;module.node
;經歷 路徑分析
、文件定位
和編譯執行
。java
路徑分析、文件定位
,會直接被加載到了內存中,其中系統模塊定義在源碼的 lib 目錄下;.、..、/
開頭的,會依次按照 .js、.json、.node
進行擴展名補足嘗試(文件沒有加上擴展名),最好仍是加上文件的擴展名。package.json
中查找;模塊緩存後,能夠經過 require.cache
查看已緩存的模塊。node
// 模塊文件 require.module.js
module.exports = {
name: 'pr',
say(){ }
}
複製代碼
// 引用模塊文件 require.cache.js
require('./require.module');
console.log('require.cache ----- ');
console.log(require.cache);
複製代碼
1.exports 與 module.exports 關係git
const exports = module.exports;
複製代碼
因此就不能改變 exports 的指向,能夠這樣github
exports.info = {
name: 'pr',
age: 30
}
module.exports = {
name: 'pr',
age: 30
}
複製代碼
模塊 moduleA.js
和 moduleB.js
兩個模塊互相引用,會怎樣?express
// moduleA.js
console.log('模塊 moduleA');
exports.name = 'moduleA name';
age = 27;
const moduleB = require('./moduleB.js');
console.log('moduleA require moduleB =>', moduleB.name);
複製代碼
// moduleB.js
console.log('模塊 moduleB');
exports.name = 'moduleB name';
const moduleA = require('./moduleA.js');
console.log('moduleB require moduleA =>', moduleA.name);
複製代碼
node moduleA.js
,會打印 模塊 moduleA
;moduleA.js
中加載 moduleB.js
,打印 模塊 moduleB
;moduleB.js
中又加載 moduleA.js
,此時模塊 moduleA.js
尚未執行完,返回模塊 moduleA.js
的 exports
對象給到模塊 moduleB.js
;moduleB.js
加載完後,其中有個 moduleA.js
中掛載了全局的變量 age
,因此能打印出來,最後將模塊 moduleB.js
的 exports
對象給到模塊 moduleA.js
;頗有意思的是,在代碼執行前,會用一個封裝器將執行代碼段封裝起來json
(function(exports, require, module, __filename, __dirname) {
// something
});
複製代碼