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

NX 20.2.1, Newly created react-monorepo & module federation setup doesn't work #29269

Closed
1 of 4 tasks
bigfish opened this issue Dec 9, 2024 · 2 comments · Fixed by #29416
Closed
1 of 4 tasks

NX 20.2.1, Newly created react-monorepo & module federation setup doesn't work #29269

bigfish opened this issue Dec 9, 2024 · 2 comments · Fixed by #29416
Assignees
Labels
scope: module federation Issues related to module federation support type: bug

Comments

@bigfish
Copy link

bigfish commented Dec 9, 2024

Current Behavior

npx create-nx-workspace react-monorepo --preset=react-monorepo --pm=pnpm

then choose:
app name = my-app
bundler = rspack
test runner = none
stylesheet = CSS
CI = later
cacheing = no

cd react-monorepo

pnpm nx g @nx/react:host shell --remotes=remote --bundler=rspack

stylesheet = css
e2e = none

succeeds

nx serve shell

FAILS with error:

NX Remote failed to start. A complete log can be found in: /Volumes/Code/react-monorepo/.nx/workspace-data/2024-12-09T15_30_47_332Z-build.log

LOG


 NX   Running target build for project remote:

- remote



> nx run remote:build:production


 NX   Cannot find module '@nx/module-federation/rspack'

Require stack:
- /Volumes/Code/react-monorepo/remote/rspack.config.ts
- /Volumes/Code/react-monorepo/remote/rspack.config.prod.ts
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/utils/resolve-user-defined-rspack-config.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/executors/rspack/lib/config.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/utils/create-compiler.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/executors/rspack/rspack.impl.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/config/schema-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/command-line/run/executor-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/devkit-internals.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/assert-workspace-validity.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/build-project-graph.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/project-graph.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/file-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/package-manager.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/package-json.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/print-help.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/command-line/run/run.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/bin/run-executor.js
Pass --verbose to see the stacktrace.

 NX   Running target build for project remote failed

Failed tasks:

- remote:build:production

So it looks like it's just missing '@nx/module-federation/rspack' package, so I triied to install it, but that didn't work. Realiized I need just @nx/module-federation so installed that, and now it works.

So I think the generators need to be updated to install this package.

Expected Behavior

Shell app is served with remotes.

GitHub Repo

No response

Steps to Reproduce

  1. npx create-nx-workspace react-monorepo --preset=react-monorepo --pm=pnpm

then choose:
app name = my-app
bundler = rspack
test runner = none
stylesheet = CSS
CI = later
cacheing = no

cd react-monorepo

  1. pnpm nx g @nx/react:host shell --remotes=remote --bundler=rspack

stylesheet = css
e2e = none

succeeds

  1. nx serve shell

FAILS with error:

NX Remote failed to start. A complete log can be found in: /Volumes/Code/react-monorepo/.nx/workspace-data/2024-12-09T15_30_47_332Z-build.log

LOG


 NX   Running target build for project remote:

- remote



> nx run remote:build:production


 NX   Cannot find module '@nx/module-federation/rspack'

Require stack:
- /Volumes/Code/react-monorepo/remote/rspack.config.ts
- /Volumes/Code/react-monorepo/remote/rspack.config.prod.ts
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/utils/resolve-user-defined-rspack-config.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/executors/rspack/lib/config.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/utils/create-compiler.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/executors/rspack/rspack.impl.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/config/schema-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/command-line/run/executor-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/devkit-internals.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/assert-workspace-validity.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/build-project-graph.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/project-graph.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/file-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/package-manager.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/package-json.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/print-help.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/command-line/run/run.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/bin/run-executor.js
Pass --verbose to see the stacktrace.

 NX   Running target build for project remote failed

Failed tasks:

- remote:build:production

So it looks like it's just missing '@nx/module-federation/rspack' package, so I triied to install it, but that didn't work. Realiized I need just @nx/module-federation so installed that, and now it works.

So I think the generators need to be updated to install this package.

Nx Report

NX   Report complete - copy this into the issue template

Node           : 22.11.0
OS             : darwin-x64
Native Target  : x86_64-macos
pnpm           : 8.10.2

nx                 : 20.2.1
@nx/js             : 20.2.1
@nx/jest           : 20.2.1
@nx/eslint         : 20.2.1
@nx/workspace      : 20.2.1
@nx/devkit         : 20.2.1
@nx/eslint-plugin  : 20.2.1
@nx/react          : 20.2.1
@nx/rspack         : 20.2.1
@nx/web            : 20.2.1
typescript         : 5.6.3
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/jest/plugin
---------------------------------------
Community plugins:
@nx/module-federation : 20.2.1

Failure Logs

NX   Cannot find module '@nx/module-federation/rspack'

Require stack:
- /Volumes/Code/react-monorepo/remote/rspack.config.ts
- /Volumes/Code/react-monorepo/remote/rspack.config.prod.ts
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/utils/resolve-user-defined-rspack-config.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/executors/rspack/lib/config.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/utils/create-compiler.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/@nx+rspack@20.2.1_@module-federation+enhanced@0.7.6_@module-federation+node@2.6.11_@swc-node+_vvolop54a2iqvroiphed6tvd2q/node_modules/@nx/rspack/src/executors/rspack/rspack.impl.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/config/schema-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/command-line/run/executor-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/devkit-internals.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/assert-workspace-validity.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/build-project-graph.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/project-graph.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/project-graph/file-utils.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/package-manager.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/package-json.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/utils/print-help.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/src/command-line/run/run.js
- /Volumes/Code/react-monorepo/node_modules/.pnpm/nx@20.2.1_@swc-node+register@1.9.2_@swc+core@1.5.29/node_modules/nx/bin/run-executor.js
Pass --verbose to see the stacktrace.

Package Manager Version

pnpm 8.10.2

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@jaysoo jaysoo added the scope: module federation Issues related to module federation support label Dec 10, 2024
@rmaclean
Copy link

The tl;dr is I suspect import { withModuleFederation } from '@nx/module-federation/rspack'; is the problem - it seems it cannot find it and if I roll back that import to '@nx/rspack/module-federation' it start being ok.

This is a similar issue for me, except I did an update to cause it, rather than fresh install. Working at the start

 NX   Report complete - copy this into the issue template

Node           : 22.12.0
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 9.15.0

nx (global)        : 20.1.0
nx                 : 20.1.4
@nx/js             : 20.1.4
@nx/jest           : 20.1.4
@nx/eslint         : 20.1.4
@nx/workspace      : 20.1.4
@nx/devkit         : 20.1.4
@nx/eslint-plugin  : 20.1.4
@nx/playwright     : 20.1.4
@nx/react          : 20.1.4
@nx/rspack         : 20.1.4
@nx/vite           : 20.1.4
@nx/web            : 20.1.4
@nx/webpack        : 20.1.4
typescript         : 5.6.3
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/vite/plugin

Did a nx migrate latest and exact same issue as above. Report from after the migrate

NX   Report complete - copy this into the issue template

Node           : 22.12.0
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 9.15.0

nx (global)        : 20.1.0
nx                 : 20.2.2
@nx/js             : 20.2.2
@nx/jest           : 20.2.2
@nx/eslint         : 20.2.2
@nx/workspace      : 20.2.2
@nx/devkit         : 20.2.2
@nx/eslint-plugin  : 20.2.2
@nx/playwright     : 20.2.2
@nx/react          : 20.2.2
@nx/rspack         : 20.2.2
@nx/vite           : 20.2.2
@nx/web            : 20.2.2
@nx/webpack        : 20.2.2
typescript         : 5.6.3
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/vite/plugin

Tried with 20.2.0 (I didn't try 20.2.1 since the above is with that) and 20.3.0-beta.0 and the same issue on those builds too

This might be useful rspack.config.ts before change

import { composePlugins, withNx, withReact } from '@nx/rspack';
import {
  withModuleFederation,
  ModuleFederationConfig,
} from '@nx/rspack/module-federation';

import baseConfig from './module-federation.config';

const config: ModuleFederationConfig = {
  ...baseConfig,
};

// Nx plugins for rspack to build config object from Nx options and context.
/**
 * DTS Plugin is disabled in Nx Workspaces as Nx already provides Typing support for Module Federation
 * The DTS Plugin can be enabled by setting dts: true
 * Learn more about the DTS Plugin here: https://module-federation.io/configure/dts.html
 */
export default composePlugins(
  withNx(),
  withReact(),
  withModuleFederation(config, { dts: false }),
  (config) => {
    config.devServer = {
      allowedHosts: 'all',
    }

    if (config.module?.rules) {
      config.module.rules = config.module.rules.filter(r => r.test.toString() !== '/\\.svg$/');
    }

    config.module?.rules?.push({
      test: /\.svg$/,
      issuer: /\.[jt]sx?$/,
      use: [
        {
          loader: require.resolve('@svgr/webpack'),
          options: {
            exportType: 'named',
            namedExport: 'ReactComponent',
          },
        },
      ],
    });

    return config;
  }
);

and after change

import { ModuleFederationConfig } from '@nx/module-federation';
import { composePlugins, withNx, withReact } from '@nx/rspack';
import { withModuleFederation } from '@nx/module-federation/rspack';

import baseConfig from './module-federation.config';

const config: ModuleFederationConfig = {
  ...baseConfig,
};

// Nx plugins for rspack to build config object from Nx options and context.
/**
 * DTS Plugin is disabled in Nx Workspaces as Nx already provides Typing support for Module Federation
 * The DTS Plugin can be enabled by setting dts: true
 * Learn more about the DTS Plugin here: https://module-federation.io/configure/dts.html
 */
export default composePlugins(
  withNx(),
  withReact(),
  withModuleFederation(config, { dts: false }),
  (config) => {
    config.devServer = {
      allowedHosts: 'all',
    };

    if (config.module?.rules) {
      config.module.rules = config.module.rules.filter(
        (r) => r.test.toString() !== '/\\.svg$/',
      );
    }

    config.module?.rules?.push({
      test: /\.svg$/,
      issuer: /\.[jt]sx?$/,
      use: [
        {
          loader: require.resolve('@svgr/webpack'),
          options: {
            exportType: 'named',
            namedExport: 'ReactComponent',
          },
        },
      ],
    });

    return config;
  },
);

@Coly010
Copy link
Contributor

Coly010 commented Dec 19, 2024

@bigfish This seems like a package resolution issue. The @nx/react, @nx/angular and @nx/rspack packages all have a direct dependency on @nx/module-federation package. So it should have been found.

We can ensure it gets installed at the package root though when running the generators.

@rmaclean Same as above. That package should have been resolved by your package manager. Rolling back the import makes sense if you also roll back the versions of the packages you're using, but moving forward, the import should be @nx/module-federation/rspack.

I'll update the generators and the migrations to forcibly install the package.

Coly010 added a commit that referenced this issue Dec 19, 2024
…lled for users (#29416)

## Current Behavior
The `@nx/module-federation` package is a direct dependency of some of
the other packages in the Nx Plugin ecosystem.
It should be resolved correctly by package managers such that it can be
used when setting up MF projects.

However, some users are facing issues with module resolutions where the
package is not found as expected.

## Expected Behavior
Install the package directly for the user to ensure module resolution
works as expected.

## Related Issue(s)

Fixes #29269
ndcunningham pushed a commit that referenced this issue Dec 20, 2024
…lled for users (#29416)

## Current Behavior
The `@nx/module-federation` package is a direct dependency of some of
the other packages in the Nx Plugin ecosystem.
It should be resolved correctly by package managers such that it can be
used when setting up MF projects.

However, some users are facing issues with module resolutions where the
package is not found as expected.

## Expected Behavior
Install the package directly for the user to ensure module resolution
works as expected.

## Related Issue(s)

Fixes #29269
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: module federation Issues related to module federation support type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants