readFileSync



  • I’ll preface this by saying I’m fairly new at nodejs, so maybe I’m doing something totally stupid. I’ve been beating my head on this for a while now… Not making any sense to me whatsoever, googling like crazy and still totally stuck.

    I have bigger plans (reading a locally updated JSON file and displaying frequently updated data to the screen), but as nothing seemed to work I kept paring and paring the project back to where I’m now simply trying to display static contents of a local file to the screen. I altered helloworld to try to do this simple task. Note that the file ‘/home/pi/test.txt’ is simply the word “test”, and has permissions set to where anyone can read it. I’m also loading other modules just so I know something’s working.

    The following code locks up the mirror, displaying simply a black screen. Note that I am not even attempting to display the file at this point, I’m simply trying to load it into the variable ‘file’.

    Module.register("helloworld",{
    
            getDom: function() {
                    var wrapper = document.createElement("div");
                    var file = require('fs').readFileSync('/home/pi/test.txt', 'utf8');
                    wrapper.innerHTML = "Hello";
                    return wrapper;
            }
    });
    

    I can tell that the issue is my loading line because I can comment it out thusly and all the modules display, including the “Hello” text:

    Module.register("helloworld",{
    
            getDom: function() {
                    var wrapper = document.createElement("div");
    //              var file = require('fs').readFileSync('/home/pi/test.txt', 'utf8');
                    wrapper.innerHTML = "Hello";
                    return wrapper;
            }
    });
    

    OK, so maybe there’s a problem with my loading statement. Easy to check, copy/past the line into a js file and run it:

    test.js:

    var file = require('fs').readFileSync('/home/pi/test.txt', 'utf8');
    console.log(file);
    

    And here’s what happens:

    pi@lugarpi:~ $ nodejs test.js
    test
    

    Works perfectly fine in nodejs but if I put that line of code into the module it locks up the mirror. Am I missing something here? Also, I’m launching mirror through pm2. I understood any errors should show up in ~/.pm2/logs but the error log doesn’t show anything.


  • MagicMirror Creator

    What does it lockup? The fronted? Or the backend?

    If it’s just the frontend, check your webinspector/console to see what’s going on.



  • That’s where my lack of knowledge on how to debug in this new environment was hurting me. Was able to figure out a better way to debug and now have something to work with.

    Uncaught ReferenceError: require is not defined
    

    Off to roll up my sleeves and do some mad google-fu.

    Thank you! :)


  • Moderator

    That’s because require isn’t part of JavaScript. It’s part of node.js to load modules. There are potentially other ways of doing what you’re trying to do. You may want to check out this post on Stack Overflow.



  • Yeah, I went about it differently than I wanted to. I ended up porting everything to an apache directory so it serves itself as a webapp. Had a few bugs to work out (it was locking up if the json was in the middle of writing when it was retrieved) but I have it working and bug free now best I can tell :)


  • Moderator

    I suppose that’s one way to do that … doesn’t make it very portable or sharable to others. It’s no longer contained within only MM and Node.js … But, if it’s working for you and does what you want, that’s the only thing that matters.


  • MagicMirror Creator

    Just so you know, the node_helper can serve files as well: https://github.com/MichMich/MagicMirror/tree/master/modules#thisexpressapp


  • Moderator

    I saw that when I was fiddling with some other stuff, but never actually tested it. Is that path relative to the actual module location? Or MM’s installation? Or the user that’s running MM? So if '/foo/bar/baz' is defined, where does it start?

    /foo/bar/baz? (OH PLEASE SAY NO, for the love of all that is sacred, tell me Node.js does NOT allow system reads like that!)
    $HOME/foo/bar/baz ?
    $HOME/MagicMirror/foo/bar/baz?
    $HOME/MagicMirror/modules/foo/bar/baz?
    $HOME/MagicMirror/modules//foo/bar/baz?
    

  • MagicMirror Creator

    If you are referring to this snippet:

    start: function() {
        this.expressApp.get('/foobar', function (req, res) {
            res.send('GET request to /foobar');
        });
    }
    

    The /foobar is referring to the url: http://localhost:8080/foobar


  • Moderator

    Ah, so it’s still an HTTP call then, not a simple fileread from disk.


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.