简体   繁体   English

Browserify要求模块中的模块

[英]Browserify Requiring Modules within Modules

I'm trying, with Browserify , to enable node.js like modules in a socket.io web client. 我想,与Browserify ,使node.js像一个模块socket.io Web客户端。 I'm having problems requiring modules within required modules. 我在要求的模块中需要模块时遇到问题。

main.js requires client.js , which requires admin.js main.js需要client.js ,而后者需要admin.js

client.js require path is relative to main.js location in the filesystem. client.js require路径相对于文件系统中main.js位置。

admin.js require path is relative to client.js location in the filesystem. admin.js require路径相对于文件系统中client.js位置。

browserify main.js -o client.packaged.js produces a file with client.js embedded. browserify main.js -o client.packaged.js生成一个嵌入了client.js的文件。 admin.js is required when an event is fired in client.js . client.js触发事件时,需要admin.js When this event fires, I get the following error: 触发此事件时,出现以下错误:

Uncaught Error: Cannot find module '../../../modules/admin/admin.js'

I've tried changing the path to be relative to the browserified bundle and relative to client.js , both result in the error above, just different paths. 我尝试将路径更改为相对于浏览器化的包以及相对于client.js的路径,两者均导致上述错误,只是路径不同。

I've got Chrome open with --alow-file-access-from-files , so I know that shouldn't be part of the problem. 我已经使用--alow-file-access-from-files打开了Chrome浏览器,因此我知道这不应该成为问题的一部分。

In the source map produced by browserify , admin.js is not anywhere, so it must not be finding it for some reason. 在所产生的源地图browserifyadmin.js是不是在任何地方,所以它不能找到它的一些原因。

How do you correctly use relative paths within modules to require other modules when using browserify ? 使用browserify时,如何正确使用模块内的相对路径来要求其他模块?

Thanks in advance for any help! 在此先感谢您的帮助!

Edit - Added source below to help clarify 编辑-在下面添加了源以帮助弄清

main.js main.js

//Include the client
var client = require('../../base/client/client.js').client;

//Start when document is ready
$(function() {
    console.log(client);
    client.start();
});

client.js client.js

var client = new Object();
client.start = function() {
    //Server specific information
    var IP = 'localhost';
    var PORT = '1337';

    //Flags
    //SSL - true for secured connections
    //DEBUG - true to enable console.log() messages
    var SSL = false;
    var DEBUG = true;

    //Locations of modules to include
    var MODULE_LOCATIONS = [
        '../../modules/admin/admin.js'
    ];

    //Builds an array of modules to start
    var MODULES = [];
    for (var i = 0; i < MODULE_LOCATIONS.length; i++) {
        MODULES.push(require(MODULE_LOCATIONS[i]));
    }

    var socket = io.connect(getConnectionString());    
    socket.on('connection', function(socket) {
        client.onConnection(socket);

        //Load modules
        for (var i = 0; i < MODULES.length; i++) {
            MODULES[i].start(socket, io);
        }
    });

    //Returns a connection string to the socket.io server
    function getConnectionString() {
        if (SSL) {
            return 'https://' + IP + ':' + PORT;
        } else {
            return 'http://' + IP + ':' + PORT;
        }
    }
};
module.exports.client = client;

admin.js admin.js

//Events
var SYSTEM_STATS = 'system_stats';
var start = function(socket, io) {
    socket.on(SYSTEM_STATS, function(data) {
        admin.onSystemStats(socket, data);
    });
}
module.exports.start = start;
//Locations of modules to include
var MODULE_LOCATIONS = [
    '../../modules/admin/admin.js'
];

//Builds an array of modules to start
var MODULES = [];
for (var i = 0; i < MODULE_LOCATIONS.length; i++) {
    MODULES.push(require(MODULE_LOCATIONS[i]));
}

So yeah, browserify does static analysis. 是的,browserify进行静态分析。 This means it reads and "understands" your code at a statement level, but it doesn't actually execute it. 这意味着它在语句级别读取和“理解”您的代码,但实际上并没有执行它。 This type of construct defeats the static analysis's capabilities and thus browserify does not detect that the client.js module depends on admin.js . 这种类型的构造会破坏静态分析的功能,因此admin.js不会检测到client.js模块依赖admin.js Remove this extra metaprogramming logic and put in a plain vanilla require('../../modules/admin/admin.js') and I think you'll be good to go. 删除这种多余的元编程逻辑,并放入普通的require('../../modules/admin/admin.js') ,我认为您会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM