ioBroker / ioBroker.web

Simple WWW web server on express for ioBroker
MIT License
48 stars 24 forks source link

WebExtensions load/require other modules #438

Open SKB-CGN opened 3 months ago

SKB-CGN commented 3 months ago

Hi, i am using WebExtensions in my adapter and wanted to require the module "mime". It is also installed via package.json and further used (trying to) inside web.js.

The Web-Adapter raises an error, regarding a dynamic module should be used. How to perform this, please?

Apollon77 commented 3 months ago

Can you please post code and the exact error message?

SKB-CGN commented 3 months ago

Sure. Error from Web-Adapter is: 2024-06-26 11:42:00.638 - error: web.0 (5494) Cannot start extension "energiefluss-erweitert.0": Error [ERR_REQUIRE_ESM]: require() of ES Module /opt/iobroker/node_modules/iobroker.energiefluss-erweitert/node_modules/mime/dist/src/index.js from /opt/iobroker/node_modules/iobroker.energiefluss-erweitert/lib/web.js not supported.

I wonder, why it is trying to load the module inside the adapter folder?!

And the code is something like: var mime = require("mime");

Later on: res.contentType(import_mime.default.getType(import_path.default.extname(normalized_filename).substring(1)) || "html");

Apollon77 commented 3 months ago

For me it sounds like that mime v4 is an ESM only solution... this means that "require" is not allowed at all. ESM packages need to be "import"ed. So try downgrading mime to v3 which should be cjs and so require is allowed.

Additonally the interesting fact is that you do not import mime but mime-types? Or is there an other version that uses mime which is not github master? ... but mime-types is not in your package json?! This is also an issue! You need to include each package that you need into the package.json as dependency

SKB-CGN commented 3 months ago

So, that means, i need to have mime@3.xx Version?

Well, i found out, that mime-types is already a dependency of web-adapter. Therefore, i thought i dont need it because my adapter is not running without web installed. Am i correct?

In the meantime, i threw out mime and using mime-type instead.

Apollon77 commented 3 months ago

Always add your dependencies you need! As you see when the extension is loaded from your adapter dir also the package dependenca look up is done from your directory. the logic works always from the file it interprets. And when you miss a dependency then it might be that you get the wrong one or non because npm structures the dependency tree in a way that dependencies are fufilled. YOu can not assume that a package is available for your scropt if you do not put it as dependency.

Any yes I would try mime @3 or adjust your script to be ESM (but better not start that)

SKB-CGN commented 3 months ago

Okay, if i understand you correctly, i need to include every package into package.json (via npm i on localmachine) and can use them everywhere - except its ESM only.

But, "normal" things like "fs" or "path" are not needed, correct?

Apollon77 commented 3 months ago

correct

SKB-CGN commented 3 months ago

Great. Thanks for confirming!

Would you mind, to shortly check web.js and package.json in my repro again?