Uninitialized exports not recognized by Webpack
See original GitHub issueWhat version of Next.js are you using?
latest
What version of Node.js are you using?
latest
What browser are you using?
Chrome
What operating system are you using?
Ubuntu
How are you deploying your application?
N/A
Describe the Bug
From #27876:
There seem to be some edge cases with export resolution that break Webpack, particularly when circularly re-importing something from an index.
Work around this by importing members from the module they were originally exported from, i.e.
import { something } from '../context/something'
instead of importing a re-export viaimport { something } from '..'
.
In this repro (an older version of a library I’m writing, which I tried to simplify into a standard a/b/c
style example, but kept losing the error when simplifying the project structure), the function useSolanaBalance
is exported before it’s actually initialized, and this seems to confuse Webpack. (Reconcile the JS output in dist/
with demo/src/pages/index.tsx
.)
What Node sees
The module exports everything as expected, and this is how hooks/useBalance
sees import * as rootStarImport from '../..'
, as tested with node dist/hooks/useBalance.js
(emphasis added to indicate uninitialized export):
inside hooks/useBalance [Module: null prototype] { BigButton: [Function: BigButton], SolanaStateProvider: [Function (anonymous)], getRpcUrl: [AsyncFunction: getRpcUrl], newAccountWithLamports: [AsyncFunction: newAccountWithLamports], useConnection: [Function: useConnection], useSolanaBalance: <uninitialized>, useSolanaState: [Function (anonymous)] }
Can also import { useSolanaBalance } from './dist/index.js'
and execute it without issue.
What Next sees
Throws:
(0 , _dist_hooks__WEBPACK_IMPORTED_MODULE_2__.useSolanaBalance) is not a function
In the console:
inside hooks/useBalance Object [Module] {
BigButton: [Getter],
getRpcUrl: [Getter],
newAccountWithLamports: [Getter]
}
CC re. resolution logic:
- @timneutkens (asked for re-export repro)
- @sokra (involves Webpack logic)
Expected Behavior
Import resolution by Webpack should directly mirror native ESM resolution at runtime (i.e., not throw *__WEBPACK_IMPORTED_MODULE_*__ is not a function
).
To Reproduce
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:8 (4 by maintainers)
Top GitHub Comments
You can try to set the webpack option
config.optimization.providedExports = true
oh yay that worked!