• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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 3.6k 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.
  • A Offline
    acimail01
    last edited by Jul 21, 2019, 10:59 AM

    Is it possible to load a config value from a file (yml, json or other)?

    modules: [
    	{
    		module: "clock",
    		position: "top_left",	// This can be any of the regions.
    		config: {
                               displayType: #LOAD_VALUE_FROM_FILE#
    		}
    	}
    ]
    
    S 1 Reply Last reply Jul 21, 2019, 1:59 PM Reply Quote 0
    • S Away
      sdetweil @acimail01
      last edited by Jul 21, 2019, 1:59 PM

      @acimail01 sure. U have to write the code, but the fs module (already loaded) provides read functions

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      I 1 Reply Last reply Jul 21, 2019, 5:54 PM Reply Quote 1
      • I Offline
        idoodler Module Developer @sdetweil
        last edited by Jul 21, 2019, 5:54 PM

        @sdetweil Attention, the fs module is (obviously) not available in the server only mode.

        1 Reply Last reply Reply Quote 0
        • A Offline
          acimail01
          last edited by Jul 21, 2019, 6:55 PM

          Here’s my attempt that does not work:
          this line causes the error: var myfs = require(‘fs’)
          what am I doing wrong ?

          pi@raspberrypi:~/MagicMirror $ cat my.json
          {
             "clockmodule": {
                          "showDate": true,
                          "displayType": "both"
                  }
          }
          
          
          

          and config.js

          var myfs = require('fs')
          //var myconfig = JSON.parse(myfs.readFileSync('./my.json', 'utf8'))
          
          var config = {
             ....
          
          	modules: [
          		{
          			module: "clock",
          			position: "top_left",
          			//showDate: myconfig.module_clock["showDate"],
          			//displayType: myconfig.module_clock["displayType"],
          		},
          

          0_1563735026845_error.png

          I 1 Reply Last reply Jul 21, 2019, 7:01 PM Reply Quote 0
          • I Offline
            idoodler Module Developer @acimail01
            last edited by Jul 21, 2019, 7:01 PM

            @acimail01 This is because the require function is not available in the browser. Are you using the server only mode?

            1 Reply Last reply Reply Quote 0
            • A Offline
              acimail01
              last edited by Jul 21, 2019, 7:21 PM

              no, it is the kiosk mode. but I also tried the serveronly mode.

              I 1 Reply Last reply Jul 21, 2019, 7:29 PM Reply Quote 0
              • I Offline
                idoodler Module Developer @acimail01
                last edited by Jul 21, 2019, 7:29 PM

                @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 Jul 21, 2019, 7:42 PM

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

                  I 1 Reply Last reply Jul 21, 2019, 8:02 PM Reply Quote 0
                  • I Offline
                    idoodler Module Developer @acimail01
                    last edited by idoodler Jul 21, 2019, 8:12 PM Jul 21, 2019, 8:02 PM

                    @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 Jul 21, 2019, 8:42 PM

                      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.

                      I 1 Reply Last reply Jul 21, 2019, 9:41 PM Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        1/12
                        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