• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

How to use "require" in a module?

Scheduled Pinned Locked Moved Development
4 Posts 3 Posters 3.6k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    mbalfour
    last edited by paviro Jul 16, 2016, 1:16 PM Jun 11, 2016, 9:07 PM

    I’m trying to make my first module. A pretty simple one, I just want to display the IP address of my Raspberry Pi. (I’m not assigning it a static addr, so if it changes I’d like to know the new addr to remote into)

    A quick google turned up some code for querying for your local IP address. However, it’s not working. I’m getting “Uncaught ReferenceError: require is not defined”. Am I missing something silly here? (Yes, I know the actual ip-getting code is commented out, I’ll bring it back once I can get the reference to os working)

    Here’s the code:

    var os = require("os");
    Module.register("my_ip", {
    
        // Default module config.
    	defaults: {
    	},
    
        // Override dom generator.
    	getDom: function () {
    	    var ip = location.host;
    
    	    /*
    	    var ifaces = os.networkInterfaces();
    
    	    Object.keys(ifaces).forEach(function (ifname) {
    	        var alias = 0;
    
    	        ifaces[ifname].forEach(function (iface) {
    	            if ('IPv4' !== iface.family || iface.internal !== false) {
    	                // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
    	                return;
    	            }
    
    	            if (alias >= 1) {
    	                // this single interface has multiple ipv4 addresses
    	                console.log(ifname + ':' + alias, iface.address);
                        ip = iface.address;
    	            } else {
    	                // this interface has only one ipv4 adress
    	                console.log(ifname, iface.address);
                        ip = iface.address;
    	            }
    	            ++alias;
    	        });
    	    });
            */
    
    	    var wrapper = document.createElement("div");
    	    wrapper.innerHTML = ip;
    		return wrapper;
    	},
    
    });
    
    1 Reply Last reply Reply Quote 0
    • S Offline
      strawberry 3.141 Project Sponsor Module Developer
      last edited by Jun 11, 2016, 9:10 PM

      take a look into the documentation on how to write a module, especially the part with node_helper

      Please create a github issue if you need help, so I can keep track

      1 Reply Last reply Reply Quote 0
      • M Offline
        mbalfour
        last edited by Jun 11, 2016, 9:39 PM

        Thanks! I’ve got it working with node_helper now. It seems a bit convoluted though, since my module now sends a socket notification to request the IP, and listens for the socket notification to refresh the local variable and redraw the DOM. Is this actually the correct way to do it?

        1 Reply Last reply Reply Quote 0
        • K Offline
          KirAsh4 Moderator
          last edited by Jun 12, 2016, 5:19 AM

          That’s how the node_helper method works, module sends a request, the node_helper.js script responds.

          A Life? Cool! Where can I download one of those from?

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Sam, technical setup by Karsten.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy