aSouchereau / MMM-CanadianPublicWeatherAlerts

Gets weather watches, warnings, and advisories for user specified regions. Using data published by Environment Canada.
MIT License
3 stars 0 forks source link

Cannot use import statement outside a module #3

Closed duprasf closed 7 months ago

duprasf commented 1 year ago

I installed the Canadian Public Weather Alerts module to my Magic Mirror and I get this error. I am familiar with JavaScript in a browser, but not with NodeJS specifically. Please let me know if this is something you experienced before or it is something on my environment.

Here is the error I receive in my docker container:

[21.02.2023 17:57.29.147] [ERROR] Whoops! There was an uncaught exception...
[21.02.2023 17:57.29.153] [ERROR] /opt/magic_mirror/modules/MMM-CanadianPublicWeatherAlerts/node_modules/axios/index.js:1
import axios from './lib/axios.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/opt/magic_mirror/modules/MMM-CanadianPublicWeatherAlerts/node_helper.js:11:15)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)

And here is my config file (redacted):

/* 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 config = {
    address: "0.0.0.0", // 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: 8888,
    ipWhitelist: [], // 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_center"
        },
        {
            module: "clock",
            position: "top_left"
        },
        {
            module: "calendar",
            position: "top_left",
            config: {
                maximumNumberOfDays: 90,
                maximumEntries: 8,
                animationSpeed: 0,
                colored: true,
                timeFormat: "absolute",
                displayRepeatingCountTitle: true,
                getRelative: 48,
                calendars: [
                    {
                        symbol: "calendar-check-o ",
                        color: "#FC54FC",
                        url: "https://www.calendarlabs.com/ical-calendar/ics/39/Canada_Holidays.ics"
                    },
                    {
                        symbol: "birthday-cake ",
                        color: "#00FF00",
                        url: "https://***/birthdays.ics"
                    },
                    {
                        url:"https://calendar.google.com/***",
                        customClass: function(event) {
                            if(event.title.indexOf('$') !== -1) {
                                // when an event title has a $, it will be displayed in red
                                return ['paiement'];
                            }
                            return [];
                        }
                    }
                ]
            }
        },
        {
            module: "weather",
            position: "top_right",
            config: {
                type: "current",
                useKMPHWind: true,
                useBeaufort: false,
                showHumidity: true,

                location: "***",
                locationID: "***",
                apiKey: "***"
            }
        },
        {
            module: "weatherforecast",
            position: "top_right",
            header: "Weather Forecast",
            config: {
                type: "forecast",
                colored: true,
                showRainAmount: true,
                showNextDayAfter: 18,
                location: "***",
                locationID: "***",
                apiKey: "***",
                appid: "***",
            }
        },
        {
            module: "MMM-CanadianPublicWeatherAlerts",
            position: "bottom_center",
            config: {
                lang: 'en',
                regions: [
                    {
                        code: "qc62"
                    },
                    {
                        code: "qc67"
                    }
                ],
                updateInterval: 60000,
                animationSpeed: 1000,
                displayInterval: 5000
            }
        },
        {
            module: 'MMM-MyWastePickup',
            position: 'top_left',
            header: 'Waste Collection',
            config: {
                collectionCalendar: 'external',
                location: 'https://***/schedule.csv',
                limitTo: 2
            }
        },
        {
            module: 'MMM-Garfield',
            position: 'bottom_right',
            config: {
                invertColors: false,
                updateInterval : 3600000,
            }
        },
    ]

};

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

Thanks

aSouchereau commented 1 year ago

Sorry for the late reply, must have missed the notification. It definitely could be something on your environment, I wasn't able to reproduce this at all. I've tested with different versions of magicmirror and npm, everything seems to be working fine.

To be honest, I'm not too familiar with NodeJs either (learned a little bit just for this project), so I'm not 100% on what the issue is, just that its some sort of discrepancy between ES Modules and CommonJS.

Fortunately, in the next update I removed the dependency on axios, which seemed to be causing your problem. It should be released sometime in the next few days. If you want, you can give it a try and let me know how it goes.