Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not handle ES modules #302

Open
1 task done
dathbe opened this issue May 16, 2024 · 6 comments
Open
1 task done

Does not handle ES modules #302

dathbe opened this issue May 16, 2024 · 6 comments

Comments

@dathbe
Copy link

dathbe commented May 16, 2024

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

  1. Start MM with both MMM-remote-control and an ES Module module active.
  2. 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

@KristjanESPERANTO
Copy link
Collaborator

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? 🙂

@dathbe
Copy link
Author

dathbe commented Sep 6, 2024

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.

@KristjanESPERANTO
Copy link
Collaborator

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?

@dathbe
Copy link
Author

dathbe commented Sep 13, 2024

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?

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.

@dathbe
Copy link
Author

dathbe commented Sep 14, 2024

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.

@dathbe
Copy link
Author

dathbe commented Sep 16, 2024

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.

KristjanESPERANTO added a commit to KristjanESPERANTO/MMM-Remote-Control that referenced this issue Nov 6, 2024
KristjanESPERANTO added a commit to KristjanESPERANTO/MMM-Remote-Control that referenced this issue Nov 18, 2024
KristjanESPERANTO added a commit to KristjanESPERANTO/MMM-Remote-Control that referenced this issue Nov 19, 2024
KristjanESPERANTO added a commit to KristjanESPERANTO/MMM-Remote-Control that referenced this issue Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants