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

WebExtensions load/require other modules #438

Open
SKB-CGN opened this issue Jun 26, 2024 · 8 comments
Open

WebExtensions load/require other modules #438

SKB-CGN opened this issue Jun 26, 2024 · 8 comments

Comments

@SKB-CGN
Copy link
Contributor

SKB-CGN commented Jun 26, 2024

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
Copy link
Contributor

Can you please post code and the exact error message?

@SKB-CGN
Copy link
Contributor Author

SKB-CGN commented Jun 26, 2024

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
Copy link
Contributor

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
Copy link
Contributor Author

SKB-CGN commented Jun 26, 2024

So, that means, i need to have [email protected] 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
Copy link
Contributor

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
Copy link
Contributor Author

SKB-CGN commented Jun 26, 2024

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
Copy link
Contributor

correct

@SKB-CGN
Copy link
Contributor Author

SKB-CGN commented Jun 26, 2024

Great. Thanks for confirming!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants