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 load config data from file?

    Scheduled Pinned Locked Moved Unsolved Troubleshooting
    12 Posts 4 Posters 4.5k Views 4 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.
    • idoodlerI Offline
      idoodler Module Developer @acimail01
      last edited by

      @acimail01 Start MagicMirror in debug mode with npm start dev, it will open the developer tools. Then look for any red lines in the console.

      If you can’t find the developer tools you can open it with CTRL-SHIFT-I

      1 Reply Last reply Reply Quote 0
      • A Offline
        acimail01
        last edited by

        Error message is:
        Uncaught ReferenceError: require is not defined at config.js:15

        idoodlerI 1 Reply Last reply Reply Quote 0
        • idoodlerI Offline
          idoodler Module Developer @acimail01
          last edited by idoodler

          @acimail01 Ok, I just got an idea.

          Place your json somewhere in the config folder and place the following function in your config.js.

          function loadJSON(filePath) {
              if (typeof module !== "undefined") {
                  return require(filePath);
              } else {
                  try {
                      return JSON.parse($.ajax({
                          type: "GET",
                          url: filePath,
                          async: false
                      }).responseText);
                  } catch (e) {
                      console.error(e);
                      return {};
                  }
              }
          }
          

          And call it like:

          modules: [
          	loadJSON("config/clock.json")
          ]
          

          This function will synchronisly load the given JSON, serializes it and returns it. This code is completly untestet, but it should work anyway:)

          Edit: I just updated my answer to also work with node.js

          1 Reply Last reply Reply Quote 0
          • A Offline
            acimail01
            last edited by

            thank you but wont work also.
            Error:
            ReferenceError: $ is not defined at loadJSON

            I want to have all API keys and passwords from a common location and not scattered in the config.js, Hence the thought with a (yml) file.
            maybe this can be solved via an array variable in the config.js or similar.

            idoodlerI 1 Reply Last reply Reply Quote 0
            • idoodlerI Offline
              idoodler Module Developer @acimail01
              last edited by idoodler

              @acimail01 Ok, we can fix the $ is not defined at loadJSON.

              Replace the function with the following which uses good old XMLHttpRequest:

              fuction loadJSON(filePath) {
                  if (typeof module !== "undefined") {
                      return require(filePath);
                  } else {
                      try {
                          var xmlhttp = new XMLHttpRequest();
                          xmlhttp.open("GET", filePath, false);
                          xmlhttp.send();
                          if (xmlhttp.status === 200) {
                              return JSON.parse(xmlhttp.responseText);
                          } else {
                              throw "Unexpected status code!";
                          }
                      } catch (e) {
                          console.error(e);
                          return {};
                      }
                  }
              }
              
              1 Reply Last reply Reply Quote 0
              • telminiT Offline
                telmini
                last edited by

                Did you manage to make this work? I’m trying to do kinda the same.

                Thanks

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