Io.js require vs require.js, round n


#1

I’m currently trying to port my little game engine editor from node/express to electron, the goal being to cut down on issues coming from file/IO in a server/client architecture. For instance, I use Tiled map editor for the maps, and I want users to be able to import them into the game project. Before, I would have to let the user choose a map, then the backend would read the map file for tilesets, and then return the tilesets, then the user would have to find those files on the filesystem before importing the map. If I use electron, I can simply import the “fs” module and load the file and the tilesets all at once.

The only dilemma is that I used requirejs for my original project and I’d prefer to keep it like that, but of course that conflicts with io.js require. I read a suggestion that I could simply reassign require to node_require or something like that, as I did in my index.html file…

<!DOCTYPE html>
<html>
    <head>
        <script>
            // Allow for requirejs to be loaded.
            node_require = require;
            delete require;
        </script>
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <link rel="stylesheet" href="js/lib/spectrum.css" />
        <link rel="stylesheet" href="js/lib/bootstrap/css/bootstrap.slate.css" />
        <script src="js/require_config.js"></script>
        <script>
            var require = require_config;
        </script>
        <script data-main="index.js" src="engine/js/lib/require.js"></script>
        <title>Editor</title>
    </head>
    <body>
        <h3>Editor</h3>
        <div id="content"></div>
    </body>
</html>

Then when I need to use the fs module, I can just use node_require(“fs”)…

define(function(require) {
    "use strict";

    var $ = require("jquery");
    var _ = require("underscore");
    var Backbone = require("backbone");

    console.log(node_require);
    console.log(require);
    var fs = node_require("fs");

    Backbone.ajax = function(request) {
        var method = request.type;
        var url = request.url;

        if (url.startsWith('/project/')) {
            if (method === "GET") {
                var files = fs.readdirSync('project/');
                _.each(files, function(file) {
                    console.log("FILE: ", file);
                });
            }
        }

        console.log("Request: ", request);
    };
    // .......

So when I console log node_require and require, it outputs the correct information, but when it comes to actually importing “fs”, it throws an error from within’ requirejs, which is where I landed, and I’m not quite sure what to try next. Any thoughts would be greatly appreciated!


Uncaught TypeError: require.config is not a function
#2

Sorry everyone, I prematurely posted… it was simply a matter of changing my require pattern for any files that use any io.js modules… By changing it to this, it works…

define([
    'jquery',
    'underscore',
    'backbone'
], function($, _, Backbone) {
    "use strict";

    var fs = node_require("fs");

    Backbone.ajax = function(request) {
        var method = request.type;
        var url = request.url;

        if (url.startsWith('/project/')) {
            if (method === "GET") {
                var out = [];

                var files = fs.readdirSync('projects/');
                _.each(files, function(file) {
                    var project_data = JSON.parse(fs.readFileSync('projects/' + file + '/project_data.json'));
                    project_data.game = JSON.parse(fs.readFileSync('projects/' + file + '/game.json'));
                    out.push(project_data);
                });

                request.success(out);
            }
        }

        console.log("Request: ", request);
    };

#3

I am also dealing with same problem. My remote application (remote url) loads all the libraries using requirejs as well. I am still not able to understand how your solution works. I will really appreciate if you can dissect the solution little bit more. I have described my problem here in this thread

Let me know if i missed anything.


#4

can you please share the contents of require_config.js file? what is require_config value?