let fs = require(‘fs‘) let req = (moduleName) => { //content代表的是文件内容 let content = fs.readFileSync(moduleName, ‘utf8‘) console.log(content + ‘\n return module.exports‘) //最后一个参数是函数的内容体 let fn = new Function(‘exports‘,‘module‘,‘require‘,‘__dirname‘,‘__filename‘,content + ‘\n return module.exports‘) let module = { exports:{} } return fn(module.exports,module,req,__dirname,__dirname) } let msg = req(‘./a.js‘) console.log(msg.msg)
原文地址:https://www.cnblogs.com/huyanluanyu/p/10127466.html
时间: 2024-11-02 20:07:08