MagicMirror: Logic in config.js breaks EVERYTHING

Platform: ANY

Node Version: ANY

MagicMirror Version: v2.

Description: Adding ANY logic to config.js gives the error message as if a config does not exist. The “config:check” script still passes.

Steps to Reproduce: Add any logic/requires to the top of config.js. The following line breaks it:

var fs = require("fs");

Expected Results: I expect to be able to add logic to the config.js file to load settings off disk from a JSON file in the same directory.

Actual Results: The “Please create a config file” screen appears.

Configuration:

/* Magic Mirror Config Sample
 *
 * By Michael Teeuw http://michaelteeuw.nl
 * MIT Licensed.
 *
 * For more information how you can configurate this file
 * See https://github.com/MichMich/MagicMirror#configuration
 *
 */

var fs = require("fs");

var config = {
	address: "localhost", // Address to listen on, can be:
	                      // - "localhost", "127.0.0.1", "::1" to listen on loopback interface
	                      // - another specific IPv4/6 to listen on a specific interface
	                      // - "", "0.0.0.0", "::" to listen on any interface
	                      // Default, when address config is left out, is "localhost"
	port: 8080,
	ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], // Set [] to allow all IP addresses
	                                                       // or add a specific IPv4 of 192.168.1.5 :
	                                                       // ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.1.5"],
	                                                       // or IPv4 range of 192.168.3.0 --> 192.168.3.15 use CIDR format :
	                                                       // ["127.0.0.1", "::ffff:127.0.0.1", "::1", "::ffff:192.168.3.0/28"],

	language: "en",
	timeFormat: 24,
	units: "metric",

	modules: [
		{
			module: "alert",
		},
		{
			module: "updatenotification",
			position: "top_bar"
		},
		{
			module: "clock",
			position: "top_left"
		},
		{
			module: "calendar",
			header: "US Holidays",
			position: "top_left",
			config: {
				calendars: [
					{
						symbol: "calendar-check",
						url: "webcal://www.calendarlabs.com/templates/ical/US-Holidays.ics"
					}
				]
			}
		},
		{
			module: "compliments",
			position: "lower_third"
		},
		{
			module: "currentweather",
			position: "top_right",
			config: {
				location: location.location,
				locationID: "",  //ID from http://bulk.openweathermap.org/sample/; unzip the gz file and find your city
				appid: "YOUR_OPENWEATHER_API_KEY"
			}
		},
		{
			module: "weatherforecast",
			position: "top_right",
			header: "Weather Forecast",
			config: {
				location: location.location,
				locationID: "5128581",  //ID from https://openweathermap.org/city
				appid: "YOUR_OPENWEATHER_API_KEY"
			}
		},
		{
			module: "newsfeed",
			position: "bottom_bar",
			config: {
				feeds: [
					{
						title: "New York Times",
						url: "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml"
					}
				],
				showSourceTitle: true,
				showPublishDate: true
			}
		},
	]

};

/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {module.exports = config;}

Additional Notes: This is a VERY straight forward reproduction case that cripples the configuration capabilities beyond what anyone should consider acceptable. If we can’t add logic to the config.js file, why is it not just a JSON file?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 30 (1 by maintainers)

Most upvoted comments

Understood at this point. I had originally (before actually looking though the project source) figured it was a server only or even a “build” time file. I was introduced to the project pretty late that night by the roommate who had brought up the fact there wasn’t an easy way to do autolocation on the gifts he was going to be giving to people in unknown locations who would not have the skills to do the configuration themselves.

I might become a regular contributor here after the pleasant experience and quick help from you guys 😃

As a fun little thing, I was thinking through other ways a project like this could be approached and whipped up my own little “clone” if you are interested in some “new” ideas for a v3 architecture: https://github.com/jacob-ebey/Mirror2

Granted it’s not thought through as long as this project, I have some ideas for how to handle modules that I will be implementing there as a POC before I bring anything here.

I merged you PR. But to add to the discussion: the reason it is javascript in stead of json, is that it allows modules to use callbacks as configuration options. The reason your example breaks is -as others say- because you insert a node.js only statement in a file that is used by both the server as well as the browser. An easy solution would have been something like this:

if (typeof module !== "undefined") {
    var fs = require('fs');
}

I greatly appreciate your speedy responses BTW 😃