universal-module-federation-plugin icon indicating copy to clipboard operation
universal-module-federation-plugin copied to clipboard

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

Open lij20 opened this issue 1 year ago • 7 comments

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 avatar Feb 16 '24 00:02 lij20

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 avatar Feb 19 '24 03:02 zhangHongEn

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

zhangHongEn avatar Feb 19 '24 03:02 zhangHongEn

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.

lij20 avatar Feb 20 '24 15:02 lij20

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

zhangHongEn avatar Feb 21 '24 03:02 zhangHongEn

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”.

lij20 avatar Feb 21 '24 04:02 lij20

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 avatar Feb 21 '24 06:02 zhangHongEn

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"

zhangHongEn avatar Feb 23 '24 03:02 zhangHongEn