Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

[module-federation-runtime] Warnings importing as a CommonJs module #13

Open
lij20 opened this issue Feb 16, 2024 · 7 comments
Open

[module-federation-runtime] Warnings importing as a CommonJs module #13

lij20 opened this issue Feb 16, 2024 · 7 comments

Comments

@lij20
Copy link
Contributor

lij20 commented Feb 16, 2024

I have a React application using Node 12, so I can not use ES modules. When I try to import the module-federation-runtime package as a Common JS module like this:

import { registerRemotes, findModule } from "module-federation-runtime/dist/node";

I get these warnings:

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

and

Error: Can’t resolve ‘module’ in ‘.../packages/widgets/node_modules/module-federation-runtime/dist/node
@lij20 lij20 changed the title Warnings importing as a CommonJs module [module-federation-runtime] Warnings importing as a CommonJs module Feb 16, 2024
@zhangHongEn
Copy link
Owner

The warning critical dependency is correct, just ignore it, the reason is the code in dist/node :

 "undefined"!=typeof __non_webpack_require__?__non_webpack_require__:require

@zhangHongEn
Copy link
Owner

Error: Can’t resolve ‘module’ 

This error is because your webpackconfig.js does not have target: "node" configured. You need to first confirm whether you need to use dist/node and whether you can directly use dist/index.js

You can also send a minimum reproducible example

@lij20
Copy link
Contributor Author

lij20 commented Feb 20, 2024

Error: Can’t resolve ‘module’ 

This error is because your webpackconfig.js does not have target: "node" configured. You need to first confirm whether you need to use dist/node and whether you can directly use dist/index.js

You can also send a minimum reproducible example

I have a React app so target: node wouldn't work, and I definitely can't use dist/index.js because I'm using Node 12 which doesn't support ES modules.

This is a legacy application that I'm trying to support, so unfortunately I can not upgrade the Node version either.

@zhangHongEn
Copy link
Owner

dist and dist/node are both umd modules, not es modules

@lij20
Copy link
Contributor Author

lij20 commented Feb 21, 2024

Isn’t the dist/index.js file an ES module? In your rollup config you have the format set to “es” (line 14).

Anyways, both the UMD module or the CJS module should work for my use case. Can you take a look at the PR that I raised to define the different entry points? It should just be an enhancement so that I can import like import … from “module-federation-runtime” instead of import … from “module-federation-runtime/dist/node/index.js”.

@zhangHongEn
Copy link
Owner

yes, I remember wrong. There is indeed an ES module present

the browser environment defaults to using es: dist/index, and dist/umd/index is also optional .
dist/node is used by the node server environment

@zhangHongEn
Copy link
Owner

zhangHongEn commented Feb 23, 2024

Although you are Node12, the ES module can still be introduced through webpack. Will there be any exceptions if you directly use the following code?

 import "module-federation-runtime"

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

No branches or pull requests

2 participants