Read the statement by Michael Teeuw here.
ES6 notation
-
Hi,
I have a “stupid” question. Is ES6 notation possible with MM? i.e.
import fetch from 'node-fetch';
I tried this before and I got errors.
import fetch from 'node-fetch'; ^^^^^^ SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1039:15) at Module._compile (node:internal/modules/cjs/loader:1074:27) at Module._extensions..js (node:internal/modules/cjs/loader:1173:10) at Module.load (node:internal/modules/cjs/loader:988:32) at Module._load (node:internal/modules/cjs/loader:829:12) at c._load (node:electron/js2c/asar_bundle:5:13343) at Module.require (node:internal/modules/cjs/loader:1012:19) at require (node:internal/modules/cjs/helpers:102:18) at loadModule (/home/mumble/MagicMirror/js/app.js:127:19) [07.09.2022 19:15.59.646] [ERROR] Whoops! There was an uncaught exception...
Declaration from node_helper,js
//const fetch = require('node-fetch') import fetch from 'node-fetch'; const moment = require("moment") const querystring = require("querystring")
If it is possible, how is it done?
-
@mumblebaj i don’t think its available in MM
by default the filetype mjs can use import
BUT mm does require(‘node_helper.js’)js/app.js
const helperPath = `${moduleFolder}/node_helper.js`; const Module = require(helperPath);
-
@sdetweil Thanks Sam. Is there any plan that it would one day be possible? Certain helper modules, newer versions that is, only supports ES6 notation. If we don’t move along with the times then we may get stuck in the “dark ages” so to say.
Older versions have security vulnerabilities etc.
-
@mumblebaj
You can use “import()”, but need some tricks.- wait enough time to finish importing
- or use promise to confirm loading finishes.
I used that trick in my MMM-Scenes module to import modular mjs instead of global injections.
-
@mumblebaj i am not in the loop on the plans for future MM designs, details…
best to open an issue and ask Mich
-
@mumblebaj
Ref this; https://forum.magicmirror.builders/topic/15761/best-practice-for-import-esm-in-mm-module?_=1662569185624It was for front mm module, but you can apply in the node_helper also. (Easier, because you don’t need to wait dom creation to inject script in node_helper, just do finish importing confirmation before first work begins in your start() method, or at least before using that module.)
-