7 Replies Latest reply on May 20, 2015 3:30 PM by jojoba

    trouble using mraa from a service initiated node script

    jojoba

      Greetings fellow edisoners!

       

      I've come across a peculiar issue and feel like the solution is close at hand. Here's the situation:

       

      I've created a service file that fires up a node script, main.js

       

      [Unit]

      Description=mrdude boot helper

      After=network.target

       

       

      [Service]

      ExecStart=/bin/su root -c 'node /home/root/projects/mrdude/client/main.js'

      Restart=always

      RestartSec=10s

      Environment=NODE_ENV=production

       

       

      [Install]

      WantedBy=multi-user.target

       

      The node script breaks when it gets the part where you load the mraa library. Here is the error when I list the status of the service

       

       

      root@mrdude:~# systemctl status mrDudeBootHelper.service -l

      ● mrDudeBootHelper.service - mrdude boot helper

         Loaded: loaded (/lib/systemd/system/mrDudeBootHelper.service; enabled)

         Active: activating (auto-restart) (Result: exit-code) since Fri 2015-05-08 22:09:53 UTC; 968ms ago

        Process: 1725 ExecStart=/bin/su root -c node /home/root/projects/mrdude/client/main.js (code=exited, status=8)

      Main PID: 1725 (code=exited, status=8)

       

      May 08 22:09:53 mrdude su[1725]: at Module.require (module.js:364:17)

      May 08 22:09:53 mrdude su[1725]: at require (module.js:380:17)

      May 08 22:09:53 mrdude su[1725]: at HardwareManager.self.createMraa (/home/root/projects/mrdude/client/modules/core/hardwareManager.js:18:10)

      May 08 22:09:53 mrdude su[1725]: at HardwareManager.self.init (/home/root/projects/mrdude/client/modules/core/hardwareManager.js:10:8)

      May 08 22:09:53 mrdude su[1725]: at new HardwareManager (/home/root/projects/mrdude/client/modules/core/hardwareManager.js:37:7)

      May 08 22:09:53 mrdude su[1725]: at Brain.self.createHardwareManager (/home/root/projects/mrdude/client/modules/core/brain.js:36:26)

      May 08 22:09:53 mrdude su[1725]: at Brain.self.init (/home/root/projects/mrdude/client/modules/core/brain.js:16:8)

      May 08 22:09:53 mrdude su[1725]: at new Brain (/home/root/projects/mrdude/client/modules/core/brain.js:39:7)

      May 08 22:09:53 mrdude systemd[1]: mrDudeBootHelper.service: main process exited, code=exited, status=8/n/a

      May 08 22:09:53 mrdude systemd[1]: Unit mrDudeBootHelper.service entered failed state.

       

      It looks as though node can't find the mraa library, which I am loading using


      mraa = require('mraa');


      What's also interesting is that I am able to load other custom javascript modules (e.g. var HardwareManager = require("./hardwareManager.js");)


      Is there perhaps an absolute path I can use to require mraa?


      Lastly, the node script runs perfectly fine when I cd into it's directory and start it up there using the node command.


      Anyone else out there encounter a similar issue? It seems like something simple I'm missing.


      Thanks!



        • 1. Re: trouble using mraa from a service initiated node script
          Intel_Alvarado

          Hi,

           

          Have you tried updating your MRAA library, the commands to do so are:

          root@edison:~# echo "src mraa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/mraa-upm.conf
          root@edison:~# opkg update
          root@edison:~# opkg install libmraa0
          

          Also, on most cases the proper way of calling the mraa library is with

          var m = require('mraa'); 
          

           

          Sergio

          • 2. Re: trouble using mraa from a service initiated node script
            jojoba

            Thank you for the quick response Sergio!

             

            As per your recommendation I updated the libmraa package and also changed my variable name to not be "mraa".

             

            However I'm still getting the same "require" library loading error:

             

            >>>May 08 22:09:53 mrdude su[1725]: at Module.require (module.js:364:17)


            Do you know if there is another way to load the mraa library into javascript other than:

             

            var m = require("mraa");

             

            Maybe with an absolute path in the argument passed to require?

             

            Thanks again.

            • 3. Re: trouble using mraa from a service initiated node script
              jhane

              you updated the C library not the node library.  check to see if you have the mraa node package installed and what version it is.

               

              jeff

              • 4. Re: trouble using mraa from a service initiated node script
                jojoba

                Hi Jeff,

                 

                After running:

                npm -v mraa

                I received:

                1.4.9

                 

                Also, the app runs perfectly fine when I cd into the project directory and launch using
                node main.js

                 

                It's just that for whatever reason, I can't load the mraa via require when main.js is called from a boot script. Weird. :\

                • 5. Re: trouble using mraa from a service initiated node script
                  jhane

                  Hmm, not sure.  However, we run a node script that uses mraa at startup also and it is working.  However, we created our service to look like the xdk-daemon.  I seem to recall having to install some modules locally rather than globally but I don't have a working system up right now to take a look at.

                   

                  jeff

                  • 6. Re: trouble using mraa from a service initiated node script
                    jojoba

                    Thanks @jhane!

                     

                    I have more data. I created a simple test service:

                     

                    [Unit]

                    Description=mrdude boot helper

                    After=network.target

                     

                     

                    [Service]

                    ExecStart=/bin/su root -c 'node /home/root/projects/bootTest/main.js'

                    Restart=always

                    RestartSec=10s

                    Environment=NODE_ENV=production

                     

                     

                    [Install]

                    WantedBy=multi-user.target

                     

                    and point it to a simple one-line node script "main.js".

                     

                    If main.js contains:

                    var h = require('http');

                     

                    And then I check the status of the service after starting it:

                    systemctl status bootTestService.service -l

                     

                    I get:

                    ● bootTestService.service - mrdude boot helper

                       Loaded: loaded (/lib/systemd/system/bootTestService.service; disabled)

                       Active: activating (auto-restart) since Wed 2015-05-20 20:52:29 UTC; 1s ago

                      Process: 1695 ExecStart=/bin/su root -c node /home/root/projects/bootTest/main.js (code=exited, status=0/SUCCESS)

                    Main PID: 1695 (code=exited, status=0/SUCCESS)

                     

                     

                    Nice!

                     

                     

                    If instead main.js contains:

                    var m = require('mraa');

                     

                    And then I check the status of the service after starting it:

                    systemctl status bootTestService.service -l

                     

                    I get:

                    ● bootTestService.service - mrdude boot helper

                      Loaded: loaded (/lib/systemd/system/bootTestService.service; disabled)

                      Active: activating (auto-restart) (Result: exit-code) since Wed 2015-05-20 20:52:56 UTC; 1s ago

                      Process: 1728 ExecStart=/bin/su root -c node /home/root/projects/bootTest/main.js (code=exited, status=8)

                    Main PID: 1728 (code=exited, status=8)

                     

                    May 20 20:52:56 foodison su[1728]: at Module.require (module.js:364:17)

                    May 20 20:52:56 foodison su[1728]: at require (module.js:380:17)

                    May 20 20:52:56 foodison su[1728]: at Object.<anonymous> (/home/root/projects/bootTest/main.js:5:9)

                    May 20 20:52:56 foodison su[1728]: at Module._compile (module.js:456:26)

                    May 20 20:52:56 foodison su[1728]: at Object.Module._extensions..js (module.js:474:10)

                    May 20 20:52:56 foodison su[1728]: at Module.load (module.js:356:32)

                    May 20 20:52:56 foodison su[1728]: at Function.Module._load (module.js:312:12)

                    May 20 20:52:56 foodison su[1728]: at Function.Module.runMain (module.js:497:10)

                    May 20 20:52:56 foodison systemd[1]: bootTestService.service: main process exited, code=exited, status=8/n/a

                    May 20 20:52:56 foodison systemd[1]: Unit bootTestService.service entered failed state.



                    So the good news is that I can seem to load core node libraries using require. The bad news is that it seems like node doesn't know where mraa is in this context. I'm guessing, as @jhane mentioned, that maybe I would need to install mraa locally. This solution could work, but seems inappropriate since mraa is ostensibly globally available, and could likely cause other issues.

                     

                    Anybody had trouble using "require('mraa')" from a service-launched node script?

                     

                    Thanks!


                    • 7. Re: trouble using mraa from a service initiated node script
                      jojoba

                      Ok an update!

                       

                      I added:

                      Environment="NODE_PATH=/usr/lib/node_modules"


                      to the [SERVICE] section in the service and now the require('mraa') line works!



                      Here's the final service:


                      [Unit]

                      Description=mrdude boot helper

                      After=network.target

                       

                       

                      [Service]

                      ExecStart=/bin/su root -c 'node /home/root/projects/bootTest/main.js'

                      Restart=always

                      RestartSec=10s

                      Environment="NODE_PATH=/usr/lib/node_modules"

                      Environment=NODE_ENV=production

                       

                       

                      [Install]

                      WantedBy=multi-user.target





                      Hope this helps somebody in the future.