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.

    Using npm module in a module

    Scheduled Pinned Locked Moved Development
    npm
    6 Posts 3 Posters 1.5k Views 3 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.
    • S Away
      sdetweil @prapicault
      last edited by

      @prapicault the nodejs npm modules are used by the require() clause

      var xxxx = require(‘nom_module_name’)

      then see the services (methods) provided there…
      see my sample module here

      https://github.com/sdetweil/SampleModule

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      lavolp3L 1 Reply Last reply Reply Quote 0
      • lavolp3L Offline
        lavolp3 Module Developer @sdetweil
        last edited by

        @prapicault In addition to that, the require method is used in node_helper.js files. As far as I know the require function does not work in the main module .js file. You need to reference the (main) script of the node module via the function getscripts there.

        All scripts included are of course installed via console: npm install NODE_MODULE

        An example of using them in both environments:

        node_helper.js file

        var NodeHelper = require('node_helper');
        var request = require('request');
        

        very easy, you can now use request as a function.

        A bit different with the main MMM-module.js file:

          // Define required scripts. Chart.js needed for the graph.
          getScripts: function() {
            return [
              'modules/MMM-Buienalarm/node_modules/chart.js/dist/Chart.bundle.js',
            ];
          },
        

        Basically, the script is appended to the one you’re writing. Now you can use the functions integrated in the referenced script.

        How to troubleshoot modules
        MMM-soccer v2, MMM-AVStock

        1 Reply Last reply Reply Quote 0
        • P Offline
          prapicault
          last edited by

          Thanks you both. I’ve managed to get going by:

          • adding a package.json file to my MM module (npm init --yes)
          • add my dependencies to this new file, then npm install
          • add a getScripts entry in my main js file, referencing the various dependencies included in my module
          lavolp3L 1 Reply Last reply Reply Quote 0
          • lavolp3L Offline
            lavolp3 Module Developer @prapicault
            last edited by lavolp3

            @prapicault said in Using npm module in a module:

            add my dependencies to this new file, then npm install

            Great!
            Instead of manually adding dependencies to package.json you can as well install the module with a --saveflag

            npm install axios --save
            

            Automatically adds it as dependency to package.json.

            npm uninstall axios --save
            

            automatically removes it from package.json.

            How to troubleshoot modules
            MMM-soccer v2, MMM-AVStock

            1 Reply Last reply Reply Quote 0
            • S Away
              sdetweil
              last edited by sdetweil

              be careful loading lots of services in the modulename.js via getscripts… all that is loaded into the browser… (and every instance of the browser used)

              load them in the node_helper via require as single instance, and move the memory and load to the server side

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • 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