Skip to content

fix(nextjs): export default from the generated next.config.ts - #705

Merged
leoortizz merged 2 commits into
firebase:mainfrom
BiAksoy:fix/nextjs-ts-config-default-export
Nov 23, 2026
Merged

leoortizz merged 2 commits into
firebase:mainfrom
BiAksoy:fix/nextjs-ts-config-default-export

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

For a project with next.config.ts, the adapter replaces the config during the build with a generated file that imports the original with an ES import (overrides.ts L46-L51) but ends with module.exports = config; (L105). TypeScript treats that file as an ES module with no default export. next build type-checks the project while the generated file is in place, so any project file that does import nextConfig from "./next.config" (a test or a script reading the config, for example) fails the App Hosting build, even though the same project builds locally:

▲ Next.js 16.3.8 (Turbopack)
✓ Running next.config.ts took 56ms

  Creating an optimized production build ...
✓ Compiled successfully in 6.9s
  Running TypeScript ...
lib/uses-config.ts(1,8): error TS1192: Module '"<app>/next.config"' has no default export.
Failed to type check.

Restoring original next config in project root

That is from running the adapter's dist/bin/build.js on main against a minimal App Router app (<app> stands for the app's absolute path). // @ts-nocheck in the generated file does not help, because the error is reported in the importing file.

The .mjs and .mts overrides already end with export default config;. This change adds .ts to that list, so module.exports is left only for .js, the one override that loads the original with require().

Loading is unaffected. Next.js compiles next.config.ts with SWC to CommonJS (v16.4.4, v15.5.28) and reads the result through interopDefault (config.ts, interop-default.ts: mod.default || mod), so either tail yields the same config object. I found no reason recorded for module.exports in the .ts branch; it dates from #309, where the review discussion covers // @ts-nocheck but not the export form.

Testing

  • Unit: the existing TypeScript case (overrides.spec.ts L293-L320) now expects export default config;. The .js, .mjs and .mts expectations are unchanged, and I compared the generated files from main and this branch for all four extensions: only .ts differs, in the last line.

  • Functional: the with-ts-config scenario builds and serves with the new override, so Next's loadConfig and next build accept it. It does not cover the import case, since the scenarios only write a config file. If you want that covered, I can add a unit case that type-checks a file importing the generated config with the TypeScript compiler API, or a scenario with an importing file (which needs a small change to run-local.ts).

  • npm run build, npm run lint:quiet and the package's npm test (unit and functional) pass locally on Node 26.13.0. CI runs Node 18 and 22, which I did not run locally.

  • I ran dist/bin/build.js against minimal App Router apps (TypeScript 5.9.3, React 19.3.1), before and after the change:

    next.config.ts Next 15.5.28 Next 16.3.8 Next 16.4.4
    export default object, plus a project file importing it main: TS1192, build fails. Fix: builds same same
    module.exports = object (CommonJS in .ts) builds on main and with the fix same same
    export default async function builds on main and with the fix same same

    In every successful build the config's custom header was in .next/routes-manifest.json, images.unoptimized was true in .next/required-server-files.json, and the original next.config.ts was restored afterwards.

Not tested: an actual App Hosting rollout.

One related thing I left alone: with next build --experimental-next-config-strip-types (Next 16.4.4), Node's native loader cannot resolve the generated file's extensionless ./next.config.original import, so Next prints a warning and falls back to its SWC loader. That happens on main as well, and the build result is the same as without the flag.

The override for next.config.ts imports the original with an ES import
but ended with `module.exports = config;`, so TypeScript saw no default
export. Next.js type-checks the project during `next build` while the
generated file is in place, and any project file that imports the
config's default export failed with TS1192.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Next.js adapter to treat .ts files as ESM when overriding the Next.js configuration. It also updates the corresponding test suite to use export default config; instead of module.exports = config;. There are no review comments to address.

@leoortizz
leoortizz merged commit f3fd7a6 into firebase:main Nov 23, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants