1

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.

Guy Messika
  • 169
  • 1
  • 15
  • What is version of `node.js`? – stdob-- Nov 23 '16 at 17:40
  • Node.js is v5.x and I'm using NW.JS in version 0.14.7 – Guy Messika Nov 24 '16 at 06:31
  • And what's the problem to keep the modules in the file system? – stdob-- Nov 24 '16 at 12:03
  • That's what I'm doing right now using the temp folder on the system. I'm just afraid my client won't let me permissions to write to this folder in some extremely safe environments. – Guy Messika Nov 24 '16 at 12:09
  • This is to support the application updates? If so, can make better use of `node-webkit autoupdater`? – stdob-- Nov 24 '16 at 12:13
  • I've built some services running with the same code the gui application opens with. and my code is loaded with hybrid encryption from firebase on real time. I'm not interested in killing the process because by all means it might not be possible to run in back on some users. – Guy Messika Nov 24 '16 at 12:26
  • But to be honest I missed the option you suggested and I should take closer look at it, thanks. – Guy Messika Nov 24 '16 at 12:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128947/discussion-between-guy-messika-and-stdob). – Guy Messika Nov 24 '16 at 12:51

1 Answers1

2

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

stdob--
  • 28,222
  • 5
  • 58
  • 73