1

我正在尝试从虚拟文件系统加载一些节点模块,但我无法使其工作

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")
})

我得到的错误是:错误:找不到模块“异步”

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

有人可以帮我解决这个请求吗?

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

4

1 回答 1

2

unionfs 的作者写道,require在新版本中不起作用node.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

于 2016-11-24T11:59:58.050 回答