next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Bug with dynamic imports and module.exports

Open aralroca opened this issue 3 years ago • 1 comments

Verify canary release

  • [X] 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:

https://github.com/aralroca/next-translate/issues/851#issuecomment-1237189461

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 avatar Sep 05 '22 15:09 aralroca

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

niels-bosman avatar Sep 17 '22 17:09 niels-bosman

Is this still an issue?

kdy1 avatar Feb 10 '23 15:02 kdy1

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 https://github.com/aralroca/next-translate/issues/851#issuecomment-1425933052

aralroca avatar Feb 10 '23 16:02 aralroca

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[bot] avatar Mar 16 '23 00:03 github-actions[bot]