question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uninitialized exports not recognized by Webpack

See original GitHub issue

What 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 via import { 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:

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

See https://github.com/ctjlewis/next-esm-re-exports.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
sokracommented, Oct 2, 2021

You can try to set the webpack option config.optimization.providedExports = true

1reaction
cyberwombatcommented, Oct 2, 2021

oh yay that worked!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can not access defined exports from the webpack bundle?
If I have a single file in the entry section for core object, exports work, in that the LIB variable has property for...
Read more >
cannot access before initialization webpack - You.com | The AI ...
This might be caused by a cyclic dependencies (i.e. moduleA imports module B and vice versa at the same time). Take a deeper...
Read more >
Module Methods - webpack
Make all exports from the dependency available in the current scope. The require label can occur before a string. The dependency must export...
Read more >
Troubleshooting Issues with the TypeScript SDK
error: ReferenceError: exports is not defined, workflowExists: false ... The solution is to modify your workflow bundler's Webpack configuration as follow:.
Read more >
A mostly complete guide to webpack 5 (2020)
Working with JavaScript's modules in webpack ... file you'll need at least a module.exports , which is the Common JS export for Node.js:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found