Skip to content

Turbopack: next build emits a syntactically invalid chunk (Identifier 'x' has already been declared) #95372

Description

@danbruegge

Link to the code that reproduces this issue

https://github.com/danbruegge/nextjs-turbopack-duplicate-identifier

To Reproduce

npm install
npm run build            # Turbopack (default) — "Compiled successfully", exit 0
npm run check-chunks     # node --check every emitted chunk -> exit 1, "INVALID: …"

npx next build --webpack && npm run check-chunks   # webpack -> exit 0, all chunks valid

app/trigger.js is lazily import()-ed by a client component. It does nothing but import one symbol from each of three public packages:

import { builder } from '@builder.io/sdk';
import { CommerceLayer } from '@commercelayer/sdk';
import { authenticate } from '@commercelayer/js-auth';
export const used = [builder, CommerceLayer, authenticate];

Current vs. Expected behavior

next build reports success (exit 0), but the chunk emitted for that module is syntactically invalid JS, it declares the same top-level identifier twice. Turbopack scope-hoists the three packages into one merged module and its renamer emits two top-level x in the same scope:

"use strict";let x; …
… var x=class extends d{…super("Token expired")…}   // js-auth's TokenError

let x + var x in one scope → SyntaxError: Identifier 'x' has already been declared. In the browser, the dynamic import() of the chunk throws and never resolves. node --check app/trigger.js on the source passes, the duplicate is introduced purely by Turbopack's merge + rename.

Expected: the emitted chunk is valid JS (as it is with --webpack).

Notes:

  • Deterministic, reproduces on every build.
  • Removing any one of the three imports makes it disappear.
  • Also reproduces on 16.3.0-canary.71. transpilePackages does not help. next build --webpack is unaffected (workaround).

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin 25.5.0
Binaries:
  Node: 22.22.2
  npm: 10.9.7
Relevant Packages:
  next: 16.2.9 (latest)
  react: 19.2.7
  react-dom: 19.2.7
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 build (local)

Additional context

Distilled from a real app that broke on a Next 15 → 16 migration. Related: #83352 (closed), same Identifier 'x' has already been declared codegen, triggered by a different package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TurbopackRelated to Turbopack with Next.js.

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions