node commonjs原理 发表于 2019-09-14 | 分类于 前端 核心思想12345678910111213141516let mod = { exports : { }}const fn = function(exports,module,require,__filename,__dirname){ this.avg = 'aaaa' module.exports = { gg : 1 } exports.xxxx = {aaa : 2} //不可以这样导出 exports = {},相当于exports指向新的引用,外部引用并没有改变 };fn.call(mod.exports,mod.exports,mod,__filename,__dirname)console.log(mod.exports) 模拟实现1234567891011121314151617181920212223242526272829303132let fs = require("fs");let path = require("path");const vm = require("vm");function Module(absPath) { this.id = absPath; this.exports = {};}const wrapper = [ "(function(exports,module,require,__filename,__dirname){", "\n})"];Module.prototype.load = function() { let script = fs.readFileSync(this.id, "utf8"); let fnStr = wrapper[0] + script + wrapper[1]; // console.log(fnStr) let fn = vm.runInThisContext(fnStr); fn.call(this.exports,this.exports, this, req, __filename, __dirname);};function req(file) { let absPath = path.resolve(__dirname, file); let module = new Module(absPath); module.load(); return module.exports;}const rest = req("./a.js");// const rest = require("./a.js");console.log(rest);