Skip to content

Uninitialized exports not recognized by Webpack #29361

@ctjlewis

Description

@ctjlewis

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.

Metadata

Metadata

Assignees

Labels

bugIssue was opened via the bug report template.please verify canaryPlease verify the issue with the latest canary branch.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions