【问题标题】:Load node modules from virtual file system从虚拟文件系统加载节点模块
【发布时间】:2016-11-23 16:17:34
【问题描述】:

我正在尝试从虚拟文件系统加载一些节点模块 但我不能让它工作

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

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

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

【问题讨论】:

  • node.js 的版本是什么?
  • Node.js 是 v5.x,我在 0.14.7 版本中使用 NW.JS
  • 将模块保存在文件系统中有什么问题?
  • 这就是我现在使用系统上的临时文件夹所做的事情。我只是担心我的客户不允许我在某些极其安全的环境中写入此文件夹。
  • 这是支持应用更新吗?如果是这样,可以更好地利用node-webkit autoupdater吗?

标签: node.js filesystems node-modules virtualfilesystem


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2016-07-12
    • 2015-03-16
    • 2019-01-10
    • 1970-01-01
    • 2022-01-27
    相关资源
    最近更新 更多