Does not handle ES modules
Opened this issue · 6 comments
OS
Raspbian 12
NodeJS Version
18.19.0
MagicMirror² Version
2.27.0
Remote Control Version
2.3.8
Did you try using just Remote Control alone with MM?
- I have and the error still happening
Description
Remote-Control will not load ES Modules, and instead throws the following errors:
0|mm | [2024-05-16 10:48:22.279] [ERROR] 2024-05-16T10:48:22 <error> ERROR! Could not load main module js file. Error found: Error [ERR_REQUIRE_ESM]: require() of ES Module /home/username/MagicMirror/modules/MMM-RAIN-MAP/MMM-RAIN-MAP.js from /home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js not supported.
0|mm | MMM-RAIN-MAP.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
0|mm | Instead either rename MMM-RAIN-MAP.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/username/MagicMirror/modules/MMM-RAIN-MAP/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
0|mm | (/home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js:323 Class.loadModuleDefaultConfig)
Expected behavior
Either the module should use import()
as suggested, or, if the module cannot handle ES Modules, it should check whether the module is an ES Module and not attempt to import it.
Current behavior
Log error as shown above.
Possible solution
?
Steps to reproduce
- Start MM with both MMM-remote-control and an ES Module module active.
- See error
Log
0|mm | [2024-05-16 10:48:22.279] [ERROR] 2024-05-16T10:48:22 <error> ERROR! Could not load main module js file. Error found: Error [ERR_REQUIRE_ESM]: require() of ES Module /home/username/MagicMirror/modules/MMM-RAIN-MAP/MMM-RAIN-MAP.js from /home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js not supported.
0|mm | MMM-RAIN-MAP.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
0|mm | Instead either rename MMM-RAIN-MAP.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/username/MagicMirror/modules/MMM-RAIN-MAP/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
0|mm | (/home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js:323 Class.loadModuleDefaultConfig)
config.js
n/a
Additional info
No response
I think that MMM-RAIN-MAP was mistakenly labelled as an ES module. I corrected that with my last PR. @dathbe can you please check this out again? 🙂
I'm getting a different error now:
0|mm | [2024-09-06 14:40:49.936] [ERROR] 2024-09-06T14:40:49 <error> ERROR! Could not validate main module js file. (/home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js:320 Class.loadModuleDefaultConfig)
0|mm | [2024-09-06 14:40:50.030] [ERROR] 2024-09-06T14:40:49 <error> ReferenceError: Log is not defined
0|mm | at Object.<anonymous> (/home/username/MagicMirror/modules/MMM-RAIN-MAP/MMM-RAIN-MAP.js:28:158622)
0|mm | at Module._compile (node:internal/modules/cjs/loader:1256:14)
0|mm | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1311:10)
0|mm | at Module.load (/home/username/MagicMirror/modules/MMM-MyScoreboard/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
0|mm | at Module._load (node:internal/modules/cjs/loader:945:12)
0|mm | at Function.c._load (node:electron/js2c/node_init:2:13672)
0|mm | at Module.require (node:internal/modules/cjs/loader:1122:19)
0|mm | at require (node:internal/modules/helpers:130:18)
0|mm | at Class.loadModuleDefaultConfig (/home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js:314:30)
0|mm | at /home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js:268:26
0|mm | at FSReqCallback.oncomplete (node:fs:200:5)
0|mm | (/home/username/MagicMirror/modules/MMM-Remote-Control/node_helper.js:321 Class.loadModuleDefaultConfig)
I haven't had time to parse it to see if I can find a solution.
I don't fully understand it yet, but at least commenting out or deleting the line var jsfile = require(filename);
in the node_helper.js
of MMM-Remote-Control seems to fix the problem. I don't see any functional limitations because of this workaround yet. Can you try this?
I don't fully understand it yet, but at least commenting out or deleting the line
var jsfile = require(filename);
in thenode_helper.js
of MMM-Remote-Control seems to fix the problem. I don't see any functional limitations because of this workaround yet. Can you try this?
Sorry, I saw this when you posted it, but didn't have time at the moment to look into it...and then promptly forgot. I'll test this when I have time.
I think that probably worked. I'm getting some other errors now, but I think they may be unrelated errors with another module. I'll test this for a bit to see if I see any adverse effects.
So far so good. I have not noticed any adverse effects, and my error logs seem to be clear now (at least on this module). So I suggest creating a PR.