Guy Messika Guy Messika - 8 days ago 6
Node.js Question

Load node modules from virtual file system

I'm trying to load some node modules from a virtual file system
but I can't make it work

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


The error I get is: Error: Cannot find module 'async'

now I was trying to use this module https://www.npmjs.com/package/app-module-path to add my virtual path but it only gets physical paths.

Can anybody help me with this pleas?

Don't worry about security I'm going to use encryption with this.

Answer

The author of unionfs writes that the require does not work in new versions of the 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

Comments