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

Bug with dynamic imports and module.exports #40231

Closed
1 task done
aralroca opened this issue Sep 5, 2022 · 4 comments · Fixed by #45836
Closed
1 task done

Bug with dynamic imports and module.exports #40231

aralroca opened this issue Sep 5, 2022 · 4 comments · Fixed by #45836
Assignees
Labels
bug Issue was opened via the bug report template.

Comments

@aralroca
Copy link
Contributor

aralroca commented Sep 5, 2022

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #44~20.04.1tux3 SMP Wed Jul 20 21:18:32 UTC 2022
Binaries:
  Node: 16.14.2
  npm: 8.19.1
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.2.5
  eslint-config-next: 12.2.5
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

The issue was found in the Next-translate library:

aralroca/next-translate#851 (comment)

Since Next.js switched from using babel to using SWC we have encountered this issue. The library uses a configuration where it has a loader where until now it was recommended to use a dynamic import.

Now, this error is displayed:

Critical dependency: the request of a dependency is an expression

It can be "solved" by changing the dynamic import to require. The problem with require is that then, the bundle grows a lot compared to the dynamic import. With the dynamic import it creates different chunks and imports only the one that is needed.

Expected Behavior

It should work module.exports with dynamic imports.

Link to reproduction

https://github.com/aralroca/next-translate/tree/master/examples/complex

To Reproduce

To reproduce it using the link:

  • Clone
  • yarn && yarn start

To reproduce it in any project:

Create a file with a dynamic import and module.export:

function loadSomethingWithDynamicImport(param) {
  return import(`something/${param}`).then(r => r.default)
}

module.exports = { loadSomethingWithDynamicImport }

Then import and use this helper in some page loader (for example inside getInitialProps)

@aralroca aralroca added the bug Issue was opened via the bug report template. label Sep 5, 2022
@niels-bosman
Copy link

Happening to me too while using the SWC transpiler (not with Babel) with the next-translate package.

@kdy1
Copy link
Member

kdy1 commented Feb 10, 2023

Is this still an issue?

@aralroca
Copy link
Contributor Author

Is this still an issue?

Yes, for example for the configuration of libraries inside next.config.js! in order to get more context about the issue, you can read this tread in next-translate aralroca/next-translate#851 (comment)

@kdy1 kdy1 self-assigned this Feb 10, 2023
ijjk pushed a commit that referenced this issue Feb 13, 2023
This PR enables `ignoreDynamic` for common js modules which is detected
by `auto_cjs` pass.


Fixes #40231.


---

I verified that with this change the test reproduction does not fail
with import error.

---------
@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 Mar 16, 2023
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

Successfully merging a pull request may close this issue.

3 participants