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.

    ES6 notation

    Scheduled Pinned Locked Moved Development
    7 Posts 3 Posters 881 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.
    • mumblebajM Offline
      mumblebaj Module Developer
      last edited by

      Hi,

      I have a “stupid” question. Is ES6 notation possible with MM? i.e. import fetch from 'node-fetch';

      I tried this before and I got errors.

      import fetch from 'node-fetch';
      ^^^^^^
      
      SyntaxError: Cannot use import statement outside a module
          at Object.compileFunction (node:vm:352:18)
          at wrapSafe (node:internal/modules/cjs/loader:1039:15)
          at Module._compile (node:internal/modules/cjs/loader:1074:27)
          at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
          at Module.load (node:internal/modules/cjs/loader:988:32)
          at Module._load (node:internal/modules/cjs/loader:829:12)
          at c._load (node:electron/js2c/asar_bundle:5:13343)
          at Module.require (node:internal/modules/cjs/loader:1012:19)
          at require (node:internal/modules/cjs/helpers:102:18)
          at loadModule (/home/mumble/MagicMirror/js/app.js:127:19)
      [07.09.2022 19:15.59.646] [ERROR] Whoops! There was an uncaught exception... 
      

      Declaration from node_helper,js

      //const fetch = require('node-fetch')
      import fetch from 'node-fetch';
      const moment = require("moment")
      const querystring = require("querystring")
      

      If it is possible, how is it done?

      Check out my modules at: https://github.com/mumblebaj?tab=repositories

      S M 3 Replies Last reply Reply Quote 0
      • S Offline
        sdetweil @mumblebaj
        last edited by

        @mumblebaj i don’t think its available in MM

        by default the filetype mjs can use import
        BUT mm does require(‘node_helper.js’)

        js/app.js

                        const helperPath = `${moduleFolder}/node_helper.js`;
                                const Module = require(helperPath);
        

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        mumblebajM 1 Reply Last reply Reply Quote 0
        • mumblebajM Offline
          mumblebaj Module Developer @sdetweil
          last edited by

          @sdetweil Thanks Sam. Is there any plan that it would one day be possible? Certain helper modules, newer versions that is, only supports ES6 notation. If we don’t move along with the times then we may get stuck in the “dark ages” so to say.

          Older versions have security vulnerabilities etc.

          Check out my modules at: https://github.com/mumblebaj?tab=repositories

          S 1 Reply Last reply Reply Quote 0
          • M Offline
            MMRIZE @mumblebaj
            last edited by

            @mumblebaj
            You can use “import()”, but need some tricks.

            • wait enough time to finish importing
            • or use promise to confirm loading finishes.

            I used that trick in my MMM-Scenes module to import modular mjs instead of global injections.

            1 Reply Last reply Reply Quote 1
            • S Offline
              sdetweil @mumblebaj
              last edited by

              @mumblebaj i am not in the loop on the plans for future MM designs, details…

              best to open an issue and ask Mich

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • M Offline
                MMRIZE @mumblebaj
                last edited by MMRIZE

                @mumblebaj
                Ref this; https://forum.magicmirror.builders/topic/15761/best-practice-for-import-esm-in-mm-module?_=1662569185624

                It was for front mm module, but you can apply in the node_helper also. (Easier, because you don’t need to wait dom creation to inject script in node_helper, just do finish importing confirmation before first work begins in your start() method, or at least before using that module.)

                mumblebajM 1 Reply Last reply Reply Quote 0
                • mumblebajM Offline
                  mumblebaj Module Developer @MMRIZE
                  last edited by

                  @MMRIZE and @sdetweil Thanks guys, Thought I would put the question out there and see what crawls out the woodwork. ;-)

                  Check out my modules at: https://github.com/mumblebaj?tab=repositories

                  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