从虚拟文件系统加载节点模块

Load node modules from virtual file system

我正在尝试从虚拟文件系统加载一些节点模块 但我做不到

var request = require('request')
  , AdmZip = require('adm-zip')
  , fs = require('fs')
  , unionfs = require('unionfs')
  , memfs = require('memfs')
  , mem = new memfs.Volume
  , data = []

unionfs.use(fs).use(mem)
unionfs.replace(fs)

var req = request({
    method: 'GET',
    uri: 'https://firebasestorage.googleapis.com/v0/b/****.appspot.com/o/node_modules.zip',
    headers: {
        "encoding": "binary"
    }
})

req.on('data', function(chunk) {
    data.push(chunk)
}).on('end', function() {
    var binary = Buffer.concat(data)
    mem.mountSync('./', {
        "node_modules.zip": binary
    })

    var zip = new AdmZip("./node_modules.zip")
    var files = {}
    zip.getEntries().forEach(function(entry) {
        if (entry.isDirectory)
            console.log(entry.entryName)
        files[entry.entryName] = entry.getData()
    });
    mem.mountSync('./node_modules', files)

    //need to get these modules dynamically
    var async = require("async")
})

我得到的错误是:错误:找不到模块'async'

现在我正尝试使用此模块 https://www.npmjs.com/package/app-module-path 添加我的虚拟路径,但它只能获取物理路径。

谁能帮我解决这个问题?

不用担心安全问题,我将对此使用加密。

unionfs 的作者写道 requirenode.js 的新版本中不起作用:

// NOTE: This does not work in new Node.js
// Now you can also do:
// require('/usr/mem/dir/hello.js');
// Hello world!
// require('/project/hello.js');
// Hello world!

https://github.com/streamich/unionfs/blob/master/examples/example.js#L41