node.js - 从虚拟文件系统加载 Node 模块

标签 node.js filesystems node-modules virtualfilesystem

我正在尝试从虚拟文件系统加载一些 Node 模块 但我无法让它发挥作用

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添加我的虚拟路径,但它只获取物理路径。

有人可以帮我解决这个问题吗?

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

最佳答案

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

关于node.js - 从虚拟文件系统加载 Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40769375/

相关文章:

javascript - 循环 API 的好方法是什么?

node.js - HEROKU 与日志流断开连接。可能会发生您在这里看不到的事件!正在尝试重新连接

json - Flutter - 保存用户数据

javascript - Uncaught Error : Module name "aaa_bbb_1_pb" has not been loaded yet for context: _. 使用 require([])

javascript - Node js 对象导出

node.js - 没有结帐流程的 Paypal 付款

javascript - Node.JS - 无法从 package.json 跨脚本导出环境变量

javascript - 在 Node JS 中只读取文件第一行的最有效方法是什么?

linux - 访问 ext3/ext4 期刊

javascript - 如何展平已发布的 NPM 模块的文件结构