kolbyjack / MMM-Wallpaper

MagicMirror module to display wallpapers from various online sources
https://magicmirror.builders/
MIT License
99 stars 31 forks source link

Fetch is not defined while using latest MagicMirror Docker image (karsten13/magicmirror:fat) #102

Open ToasterDEV opened 5 months ago

ToasterDEV commented 5 months ago

Hello there!

Happy user of your module here, though sadly having some problems after reformatting my RPi4B and trying to redo my setup.

Even though the module's up to date (so this) fix is already applied, I'm getting the following error in the container:

2024-01-18T17:55:14.938758748Z > magicmirror@2.23.0 start
2024-01-18T17:55:14.938794341Z > DISPLAY="${DISPLAY:=:0}" ./node_modules/.bin/electron js/electron.js
2024-01-18T17:55:14.938807693Z
2024-01-18T17:55:18.567936135Z [18.01.2024 17:55.18.564] [LOG]   Starting MagicMirror: v2.23.0
2024-01-18T17:55:18.575083968Z [18.01.2024 17:55.18.574] [LOG]   Loading config ...
2024-01-18T17:55:18.580681228Z [18.01.2024 17:55.18.579] [DEBUG] config template file not exists, no envsubst
2024-01-18T17:55:18.588648265Z [18.01.2024 17:55.18.588] [LOG]   Loading module helpers ...
2024-01-18T17:55:19.101458098Z [18.01.2024 17:55.19.100] [ERROR] (node:47) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined
2024-01-18T17:55:19.101566820Z     at Object.<anonymous> (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:13:9)
2024-01-18T17:55:19.101603116Z     at Module._compile (node:internal/modules/cjs/loader:1141:14)
2024-01-18T17:55:19.101619931Z     at Module._extensions..js (node:internal/modules/cjs/loader:1196:10)
2024-01-18T17:55:19.101632857Z     at Module.load (node:internal/modules/cjs/loader:1011:32)
2024-01-18T17:55:19.101645931Z     at Module._load (node:internal/modules/cjs/loader:846:12)
2024-01-18T17:55:19.101658339Z     at f._load (node:electron/js2c/asar_bundle:2:13330)
2024-01-18T17:55:19.101672727Z     at Module.require (node:internal/modules/cjs/loader:1035:19)
2024-01-18T17:55:19.101685153Z     at require (node:internal/modules/cjs/helpers:102:18)
2024-01-18T17:55:19.101698264Z     at loadModule (/opt/magic_mirror/js/app.js:184:19)
2024-01-18T17:55:19.101711339Z     at loadNextModule (/opt/magic_mirror/js/app.js:220:6)
2024-01-18T17:55:19.101724431Z     at /opt/magic_mirror/js/app.js:230:4
2024-01-18T17:55:19.101736135Z     at new Promise (<anonymous>)
2024-01-18T17:55:19.101749320Z     at loadModules (/opt/magic_mirror/js/app.js:211:10)
2024-01-18T17:55:19.101761450Z     at App.start (/opt/magic_mirror/js/app.js:277:9)
2024-01-18T17:55:19.101773283Z     at process.processTicksAndRejections (node:internal/process/task_queues:96:5)
2024-01-18T17:55:19.101785690Z (Use `electron --trace-warnings ...` to show where the warning was created)
2024-01-18T17:55:19.104127431Z [18.01.2024 17:55.19.103] [ERROR] (node:47) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Which I'm only able to get to go away by commenting the lines that refer to fetch from the node_helper.js file, at which point Electron loads, but the is no wallpaper displayed at all.

Is there anything else I should take into account?

Thanks for the help!

kolbyjack commented 5 months ago

Can you try adding

console.log("fetch", fetch);
console.log("typeof(fetch)", typeof(fetch));

above the if (typeof(fetch) === "undefined") line, and reporting back what's logged?

ToasterDEV commented 5 months ago

Can you try adding

console.log("fetch", fetch);
console.log("typeof(fetch)", typeof(fetch));

above the if (typeof(fetch) === "undefined") line, and reporting back what's logged?

Sure!

After adding the line, it seems nothing changed at first...

2024-01-19T19:15:36.038711887Z copy default modules to host ...
2024-01-19T19:15:36.411456125Z copy css files to host ...
2024-01-19T19:15:36.439188219Z chown modules and config folder ...
2024-01-19T19:15:36.520159381Z timezone is "America/Mexico_City"
2024-01-19T19:15:36.596915211Z start magicmirror
2024-01-19T19:15:38.804644088Z
2024-01-19T19:15:38.804728995Z > magicmirror@2.23.0 start
2024-01-19T19:15:38.804742272Z > DISPLAY="${DISPLAY:=:0}" ./node_modules/.bin/electron js/electron.js
2024-01-19T19:15:38.804753532Z
2024-01-19T19:15:41.847000907Z [19.01.2024 19:15.41.840] [LOG]   Starting MagicMirror: v2.23.0
2024-01-19T19:15:41.865478899Z [19.01.2024 19:15.41.864] [LOG]   Loading config ...
2024-01-19T19:15:41.875467132Z [19.01.2024 19:15.41.873] [DEBUG] config template file not exists, no envsubst
2024-01-19T19:15:41.887976298Z [19.01.2024 19:15.41.887] [LOG]   Loading module helpers ...
2024-01-19T19:15:42.014368861Z [19.01.2024 19:15.42.012] [LOG]   Initializing new module helper ...
2024-01-19T19:15:42.025753126Z [19.01.2024 19:15.42.024] [LOG]   Module helper loaded: MMM-Logging
2024-01-19T19:15:43.101018918Z [19.01.2024 19:15.43.100] [ERROR] 2024-01-19T19:15:43 <error> (node:49) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined
2024-01-19T19:15:43.101161769Z     at Object.<anonymous> (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:12:22)
2024-01-19T19:15:43.101186528Z     at Module._compile (node:internal/modules/cjs/loader:1141:14)
2024-01-19T19:15:43.101200176Z     at Module._extensions..js (node:internal/modules/cjs/loader:1196:10)
2024-01-19T19:15:43.101212102Z     at Module.load (node:internal/modules/cjs/loader:1011:32)
2024-01-19T19:15:43.101223917Z     at Module._load (node:internal/modules/cjs/loader:846:12)
2024-01-19T19:15:43.101235454Z     at f._load (node:electron/js2c/asar_bundle:2:13330)
2024-01-19T19:15:43.101247398Z     at Module.require (node:internal/modules/cjs/loader:1035:19)
2024-01-19T19:15:43.101259528Z     at require (node:internal/modules/cjs/helpers:102:18)
2024-01-19T19:15:43.101271083Z     at loadModule (/opt/magic_mirror/js/app.js:184:19)
2024-01-19T19:15:43.101282491Z     at loadNextModule (/opt/magic_mirror/js/app.js:220:6)
2024-01-19T19:15:43.101294009Z     at loadNextModule (/opt/magic_mirror/js/app.js:222:6)
2024-01-19T19:15:43.101305602Z     at /opt/magic_mirror/js/app.js:230:4
2024-01-19T19:15:43.101316972Z     at new Promise (<anonymous>)
2024-01-19T19:15:43.101328509Z     at loadModules (/opt/magic_mirror/js/app.js:211:10)
2024-01-19T19:15:43.101340120Z     at App.start (/opt/magic_mirror/js/app.js:277:9)
2024-01-19T19:15:43.101351805Z     at process.processTicksAndRejections (node:internal/process/task_queues:96:5)
2024-01-19T19:15:43.101363768Z (Use `electron --trace-warnings ...` to show where the warning was created) (/opt/magic_mirror/node:internal/process/warning:50 writeOut)
2024-01-19T19:15:43.109562250Z [19.01.2024 19:15.43.108] [ERROR] 2024-01-19T19:15:43 <error> (node:49) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (/opt/magic_mirror/node:internal/process/warning:50 writeOut)
2024-01-19T19:15:43.796142197Z [19.01.2024 19:15.43.776] [LOG]   Launching application.
2024-01-19T19:15:43.826858098Z [78:0119/191543.825577:WARNING:sandbox_linux.cc(380)] InitializeSandbox() called with multiple threads in process gpu-process.
2024-01-19T19:15:44.139238166Z [49:0119/191544.138947:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable.

However, if I try to go into the Docker container itself and launch it from there by running docker exec -it mm bash and then node serveronly, everything runs correctly. It seems the problem only happens when starting from npm.

[19.01.2024 19:24.10.674] [LOG]   Starting MagicMirror: v2.23.0
[19.01.2024 19:24.10.682] [LOG]   Loading config ...
[19.01.2024 19:24.10.686] [DEBUG] config template file not exists, no envsubst
[19.01.2024 19:24.10.692] [LOG]   Loading module helpers ...
[19.01.2024 19:24.10.747] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.10.749] [LOG]   Module helper loaded: MMM-Logging
[19.01.2024 19:24.11.122] [LOG]   2024-01-19T19:24:11 <log> fetch async function fetch(input, init = undefined) {
    return lazyUndici().fetch(input, init);
  } (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:12 Object.<anonymous>)
[19.01.2024 19:24.11.127] [LOG]   2024-01-19T19:24:11 <log> typeof(fetch) function (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:13 Object.<anonymous>)
[19.01.2024 19:24.11.131] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.136] [LOG]   Module helper loaded: MMM-Wallpaper
[19.01.2024 19:24.11.142] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.144] [LOG]   Module helper loaded: MMM-BurnIn
[19.01.2024 19:24.11.265] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.267] [LOG]   Module helper loaded: MMM-Remote-Control
[19.01.2024 19:24.11.270] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.271] [LOG]   Module helper loaded: MMM-Remote-Control-Repository
[19.01.2024 19:24.11.273] [LOG]   No helper found for module: MMM-AutoDimmer.
[19.01.2024 19:24.11.275] [LOG]   No helper found for module: MMM-Carousel.
[19.01.2024 19:24.11.278] [LOG]   No helper found for module: alert.
[19.01.2024 19:24.11.289] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.289] [LOG]   Module helper loaded: updatenotification
[19.01.2024 19:24.11.290] [LOG]   No helper found for module: clock.
[19.01.2024 19:24.11.545] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.546] [LOG]   Module helper loaded: calendar
[19.01.2024 19:24.11.547] [LOG]   No helper found for module: compliments.
[19.01.2024 19:24.11.548] [LOG]   No helper found for module: weather.
[19.01.2024 19:24.11.618] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.620] [LOG]   Module helper loaded: newsfeed
[19.01.2024 19:24.11.623] [LOG]   Initializing new module helper ...
[19.01.2024 19:24.11.624] [LOG]   Module helper loaded: MMM-mmpm
[19.01.2024 19:24.11.633] [LOG]   All module helpers loaded.
[19.01.2024 19:24.11.670] [LOG]   Starting server on port 8080 ...
[19.01.2024 19:24.11.678] [WARN]  You're using a full whitelist configuration to allow for all IPs
[19.01.2024 19:24.11.697] [LOG]   Server started ...
[19.01.2024 19:24.11.699] [LOG]   Connecting socket for: MMM-Logging
[19.01.2024 19:24.11.702] [LOG]   2024-01-19T19:24:11 <log> Module helper started for MMM-Logging (/opt/magic_mirror/modules/MMM-Logging/node_helper.js:19 Class.start)
[19.01.2024 19:24.11.702] [LOG]   Connecting socket for: MMM-Wallpaper
[19.01.2024 19:24.11.704] [LOG]   2024-01-19T19:24:11 <log> Starting node helper for: MMM-Wallpaper (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:62 Class.start)
[19.01.2024 19:24.11.705] [LOG]   Connecting socket for: MMM-BurnIn
[19.01.2024 19:24.11.706] [LOG]   Connecting socket for: MMM-Remote-Control
[19.01.2024 19:24.11.707] [LOG]   Starting node helper for: MMM-Remote-Control
[19.01.2024 19:24.11.714] [LOG]   Connecting socket for: MMM-Remote-Control-Repository
[19.01.2024 19:24.11.715] [LOG]   Connecting socket for: updatenotification
[19.01.2024 19:24.11.715] [LOG]   Starting module helper: updatenotification
[19.01.2024 19:24.11.716] [LOG]   Connecting socket for: calendar
[19.01.2024 19:24.11.718] [LOG]   Starting node helper for: calendar
[19.01.2024 19:24.11.719] [LOG]   Connecting socket for: newsfeed
[19.01.2024 19:24.11.720] [LOG]   Starting node helper for: newsfeed
[19.01.2024 19:24.11.721] [LOG]   Connecting socket for: MMM-mmpm
[19.01.2024 19:24.11.722] [LOG]   Starting module helper: MMM-mmpm
[19.01.2024 19:24.11.723] [LOG]   Sockets connected & modules started ...
[19.01.2024 19:24.11.724] [LOG]
Ready to go! Please point your browser to: http://0.0.0.0:8080

Then again, this line seems suspicious:

[19.01.2024 19:24.11.122] [LOG]   2024-01-19T19:24:11 <log> fetch async function fetch(input, init = undefined) {
    return lazyUndici().fetch(input, init);
  } (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:12 Object.<anonymous>)
[19.01.2024 19:24.11.127] [LOG]   2024-01-19T19:24:11 <log> typeof(fetch) function (/opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:13 Object.<anonymous>)

Where could the problem be?

Andoramb commented 5 months ago

+1 here for this, though I had no time to investigate. But I'm running directly on host, not in docker

kolbyjack commented 5 months ago

Can you try the https://github.com/kolbyjack/MMM-Wallpaper/tree/conditional-fetch-import branch?

ToasterDEV commented 5 months ago

Can you try the conditional-fetch-import branch?

Sure!

This was the result:

node@RPi4-01-PVE:/opt/magic_mirror$ node serveronly
[27.01.2024 22:57.15.533] [LOG]   Starting MagicMirror: v2.23.0
[27.01.2024 22:57.15.538] [LOG]   Loading config ...
[27.01.2024 22:57.15.546] [DEBUG] config template file not exists, no envsubst
[27.01.2024 22:57.15.557] [LOG]   Loading module helpers ...
[27.01.2024 22:57.15.612] [LOG]   Initializing new module helper ...
[27.01.2024 22:57.15.614] [LOG]   Module helper loaded: MMM-Logging
[27.01.2024 22:57.15.626] [ERROR] Whoops! There was an uncaught exception...
[27.01.2024 22:57.15.630] [ERROR] /opt/magic_mirror/modules/MMM-Wallpaper/node_helper.js:13
  const fetch = await import("fetch");
                ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at loadModule (/opt/magic_mirror/js/app.js:184:19)
    at loadNextModule (/opt/magic_mirror/js/app.js:220:6)
    at loadNextModule (/opt/magic_mirror/js/app.js:222:6)
    at /opt/magic_mirror/js/app.js:230:4
    at new Promise (<anonymous>)
    at loadModules (/opt/magic_mirror/js/app.js:211:10)
    at App.start (/opt/magic_mirror/js/app.js:277:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[27.01.2024 22:57.15.631] [ERROR] MagicMirror² will not quit, but it might be a good idea to check why this happened. Maybe no internet connection?
[27.01.2024 22:57.15.632] [ERROR] If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues