如果我将文件的内容作为字符串存储在内存中而不将其写出到磁盘,该如何请求文件?这是一个例子:
// Load the file as a string var strFileContents = fs.readFileSync( "./myUnalteredModule.js", 'utf8' ); // Do some stuff to the files contents strFileContents[532] = '6'; // Load it as a node module (how would I do this?) var loadedModule = require( doMagic(strFileContents) );
function requireFromString(src, filename) { var Module = module.constructor; var m = new Module(); m._compile(src, filename); return m.exports; } console.log(requireFromString('module.exports = { test: 1}'));
在module.js中查看_compile,_extensions和_load