Skip to content

[turbopack] dynamic imports fallback to single existing file when target file does not exist #74664

Closed
@qnten

Description

@qnten

Link to the code that reproduces this issue

https://github.com/qnten/reproduction-dynamic-import

To Reproduce

  1. Start the application in dev with turbo enabled npm run dev
  2. The page / should display the following:
1. first file exists so "first name" should be displayed: first file
2. second file does not exist so "no name found" should be displayed: first file
3. third file does not exist so "no name found" should be displayed: first file

Current vs. Expected behavior

Current Behavior

When using dynamic import to load files from a directory that contains only one file, the import incorrectly falls back to that existing file even if the requested file does not match the import path. Specifically, in the provided example:
• First File (first.ts): Exists and correctly returns "first name".
• Second File (second.ts): Does not exist, but getExportFromFile("second") still returns "first name" instead of "no name found".
• Third File (third.ts): Does not exist, but getExportFromFile("third") also returns "first name" instead of "no name found".

This behavior occurs only when the directory contains a single file. If a second file is added (second.ts) everything works as expected.

Expected Behavior

The dynamic import should strictly attempt to load the specified file based on the provided path. If the target file does not exist within the directory, the import should throw.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 22.4.1
  npm: 10.8.1
  Yarn: 1.22.22
  pnpm: 9.5.0
Relevant Packages:
  next: 15.2.0-canary.1 // Latest available version is detected (15.2.0-canary.1).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    TurbopackRelated to Turbopack with Next.js.linear: turbopackConfirmed issue that is tracked by the Turbopack team.locked

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions