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

Fails to dynamic import a module in a mono repo #37484

Closed
1 task done
FabienMotte opened this issue Jun 6, 2022 · 4 comments
Closed
1 task done

Fails to dynamic import a module in a mono repo #37484

FabienMotte opened this issue Jun 6, 2022 · 4 comments
Labels
bug Issue was opened via the bug report template.

Comments

@FabienMotte
Copy link

FabienMotte commented Jun 6, 2022

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

$ npx --no-install next info
    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 20.6.0: Tue Apr 19 21:04:45 PDT 2022; root:xnu-7195.141.29~1/RELEASE_X86_64
    Binaries:
      Node: 16.10.0
      npm: 7.24.0
      Yarn: 1.18.0
      pnpm: N/A
    Relevant packages:
      next: 12.1.6
      react: 18.1.0
      react-dom: 18.1.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

It fails to compile when trying to run a React InstantSearch Hooks with Next.js (and React 18) example in a mono repo: https://github.com/algolia/react-instantsearch/tree/feat/react-18-examples/examples/hooks-next

In React InstantSearch Hooks server module, we use two dynamic imports to import the correct react-dom/server dependency because the import varies depending on the installed React version (17 vs 18).

Next.js fails to resolve the first import and catch doesn't get called.

Screenshot 2022-06-06 at 16 59 38

Expected Behavior

Running the example locally or in a CodeSandbox, everything is working fine: https://codesandbox.io/s/friendly-swartz-jfk4nj

Is Next.js failing to resolve the dynamic import because of the mono repo architecture?

To Reproduce

Steps to reproduce the issue in the mono repo:

  1. Clone the repo: git clone git@github.com:algolia/react-instantsearch.git
  2. Checkout the relevant branch: cd react-instantsearch && git checkout feat/react-18-examples
  3. Install the dependencies: yarn
  4. Build the packages: yarn build
  5. Run the example: yarn workspace hooks-next-example dev or cd examples/hooks-next && yarn dev
  6. Open the preview: http://localhost:3000/

Thank you!

@FabienMotte FabienMotte added the bug Issue was opened via the bug report template. label Jun 6, 2022
@balazsorban44
Copy link
Member

balazsorban44 commented Jun 7, 2022

I believe this is not a Next.js issue, but rather how Node.js tries to resolve your imports here:

https://github.com/algolia/react-instantsearch/blob/76b1d3e2f16763511f56d64933386e65a52c743b/packages/react-instantsearch-hooks-server/src/getServerState.tsx#L184-L204

You can do conditional loading, something like this should work:

React.version.startsWith("18")
  ? import("react-dom/server")
  : import("react-dom/server.js")

@FabienMotte
Copy link
Author

Hello @balazsorban44 and thanks for your message!

We tried to load the module using a condition based on the React version in this commit:

algolia/react-instantsearch@195dbe8

We tried with all versions of "Next" in 12.x.x, but we either have the import error or a TS error (the dependency is already installed):

image

image

@FabienMotte
Copy link
Author

The issue is related to webpack/webpack#13865 and not Next.js, I'm closing this issue.

Thanks for your help!

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants