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.

    include npm module like firebase module into our own MM module

    Scheduled Pinned Locked Moved Development
    9 Posts 2 Posters 2.2k 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.
    • K Offline
      kevung
      last edited by

      How to include npm module like firebase module into our own MM module?
      i tried include module using const firebase = require(‘firebase’). But keep getting error

      ? 1 Reply Last reply Reply Quote 0
      • ? Offline
        A Former User @kevung
        last edited by

        @kevung
        You need node_helper.js to include node modules to your MM module. MM module itself is front-end javascript thus it cannot access your system resources. node_helper.js can help your job for handling system (e.g. File I/O, using node_modules, execution another process, etc.)

        1 Reply Last reply Reply Quote 0
        • K Offline
          kevung
          last edited by kevung

          @sean said in include npm module like firebase module into our own MM module:

          i tried node helper. below code is working. But once i added const firebase = require (‘firebase’) on top then error appeared.

          var NodeHelper = require("node_helper")
          
          module.exports = NodeHelper.create({
            start: function() {
              this.countDown = 10000000
            },
            socketNotificationReceived: function(notification, payload) {
              switch(notification) {
                case "DO_YOUR_JOB":
                  this.sendSocketNotification("I_DID", (this.countDown - payload))
                  break
              }
            },
          })
          

          The error is

          WARNING! Could not load config file. Starting with default configuration. Error found: Error: Failed to load gRPC binary module because it was not installed for the current system
          Expected directory: electron-v2.0-linux-arm-glibc
          Found: [node-v59-linux-arm-glibc]
          This problem can often be fixed by running "npm rebuild" on the current system
          Original error: Cannot find module '/home/pi/MagicMirror/modules/MMM-hellotsuhan/node_modules/grpc/src/node/extension_binary/electron-v2.0-linux-arm-glibc/grpc_node.node'
          

          I tried npm rebuild. but still not working

          ? 2 Replies Last reply Reply Quote 0
          • ? Offline
            A Former User @kevung
            last edited by

            @kevung
            I know what it is.
            MagicMirror standalone(kiosk) mode is running on Electron - frontend GUI framework. Unfortunately, it has its own node.js and chrome engine. Your current node module might not be compatible with Electron version. So you should re-build node modules for matching proper Electron.
            Normally electron-rebuild will be a help.

            In SERVERONLY mode, with Chrome browser as frontend, you might not get this error.

            1 Reply Last reply Reply Quote 0
            • ? Offline
              A Former User @kevung
              last edited by

              @kevung

              cd ~/MagicMirror/modules/MMM-hellotsuhan
              npm install --save-dev electron-rebuild
              ./node_modules/.bin/electron-rebuild
              

              This could be the solution. or not. :D

              1 Reply Last reply Reply Quote 0
              • K Offline
                kevung
                last edited by kevung

                Thanks bro!:smiling_face_with_open_mouth_smiling_eyes: this work for me!! after did below code then success to run the code already.

                cd ~/MagicMirror/modules/MMM-hellotsuhan
                npm install --save-dev electron-rebuild
                ./node_modules/.bin/electron-rebuild
                

                Now my node_helper.js is :

                const firebase = require ('firebase')
                var NodeHelper = require("node_helper")
                let countdown;
                var ref;
                var config = {
                    apiKey: "AIzaSyAxi_kFKM3lxMZ9gnBMhwoPa7pxKX_e1BI",
                    authDomain: "customerservice-ee487.firebaseapp.com",
                    databaseURL: "https://customerservice-ee487.firebaseio.com",
                    projectId: "customerservice-ee487",
                    storageBucket: "customerservice-ee487.appspot.com",
                    messagingSenderId: "466622230094"
                };
                if (!firebase.apps.length) {
                    firebase.initializeApp(config);
                }
                module.exports = NodeHelper.create({
                    start: function() {
                       ref = firebase.app().database().ref().child('countdown');
                       r ef.once('value').then(function (snap) {
                         console.log('snap.val()', snap.val());
                         countdown=snap.val();
                       });
                  },
                  socketNotificationReceived: function(notification, payload) { 
                      switch(notification) {
                         case "DO_YOUR_JOB":   
                           ref.once('value').then(function (snap) {
                              console.log('snap.val()', snap.val());
                	      countdown=snap.val();
                	  });
                          this.sendSocketNotification("I_DID", countdown - payload);      
                	  break
                      }
                  }, 	
                })
                

                My MMM-hellotsuhan.js

                Module.register("MMM-hellotsuhan",{
                    start: function (){
                        this.count = 0
                    },
                    getDom: function() {
                        var element = document.createElement("div")
                	element.className = "myContent"
                	element.innerHTML = "Hello, World! " + this.config.foo
                        var subElement = document.createElement("p")
                	subElement.id = "COUNT"
                	element.appendChild(subElement)
                	return element
                    },
                    notificationReceived: function(notification, payload, sender) { 
                        switch(notification) {
                	    case "DOM_OBJECTS_CREATED":
                	        var timer = setInterval(()=>{
                	            this.sendSocketNotification("DO_YOUR_JOB", this.count)
                	            this.count++
                	        }, 1000)
                	        break
                	    }
                    },
                    socketNotificationReceived: function(notification, payload) {
                        switch(notification) {
                	    case "I_DID":
                	        var elem = document.getElementById("COUNT")
                	        elem.innerHTML = "Count:" + payload
                	        break
                	   }
                        },
                });	
                
                K 1 Reply Last reply Reply Quote 0
                • K Offline
                  kevung @kevung
                  last edited by

                  btw Thanks for the great tutorial for developing module from @Sean !!
                  Head first developing MM module for extreme beginners

                  ? 1 Reply Last reply Reply Quote 0
                  • ? Offline
                    A Former User @kevung
                    last edited by A Former User

                    @kevung
                    I think you’d better define config in module js instead node_helper.js to be able to be redefined by user in config.js. You can move config values with sendSocketNotification.

                    K 1 Reply Last reply Reply Quote 0
                    • K Offline
                      kevung @Guest
                      last edited by

                      @sean ok will try it out later =)

                      1 Reply Last reply Reply Quote 0

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better 💗

                      Register Login
                      • 1 / 1
                      • First post
                        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