[PR #8178] [MERGED] fix(nextjs): use explicit .js extension in next/headers imports #16044

Closed
opened 2026-04-13 10:21:49 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8178
Author: @Jordanburch101
Created: 2/26/2026
Status: Merged
Merged: 2/27/2026
Merged by: @himself65

Base: canaryHead: fix/nextjs-16-esm-import


📝 Commits (1)

  • dc8a1fb fix(nextjs): use explicit .js extension in next/headers imports

📊 Changes

1 file changed (+3 additions, -3 deletions)

View changed files

📝 packages/better-auth/src/integrations/next-js.ts (+3 -3)

📄 Description

Summary

  • Adds .js extension to all three import("next/headers") calls in nextCookies() plugin
  • Bare specifier "next/headers" fails to resolve under Next.js 16's stricter ESM module resolution, especially when using bun as the runtime
  • This causes chunk resolution errors during production builds (Failed to load chunk server/chunks/ssr/...)

Details

Next.js 16 enforces proper ESM specifiers for dynamic imports. The bare "next/headers" works with Node.js resolution but fails with bun's runtime and Turbopack's stricter chunking. Adding the .js extension makes the import resolve correctly in all environments.

Before:

const { cookies } = await import("next/headers");

After:

const { cookies } = await import("next/headers.js");

Fixes #6781

Test plan

  • Verified fix resolves the build error in a Next.js 16 + bun monorepo
  • Existing tests should pass — no behavioral change, only import resolution

Summary by cubic

Switch nextCookies to use import("next/headers.js") instead of import("next/headers") so Next.js 16 resolves the module correctly and production builds don’t fail with chunk loading errors, especially on bun/Turbopack.

  • Bug Fixes
    • Replace all dynamic imports of "next/headers" with "next/headers.js".
    • Resolves build-time “Failed to load chunk” errors in Next.js 16 with bun/Turbopack.
    • No behavior change; only fixes ESM import resolution.

Written for commit dc8a1fbac0. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8178 **Author:** [@Jordanburch101](https://github.com/Jordanburch101) **Created:** 2/26/2026 **Status:** ✅ Merged **Merged:** 2/27/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/nextjs-16-esm-import` --- ### 📝 Commits (1) - [`dc8a1fb`](https://github.com/better-auth/better-auth/commit/dc8a1fbac05e32b7dd16e9454518eefe72535ac7) fix(nextjs): use explicit .js extension in next/headers imports ### 📊 Changes **1 file changed** (+3 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/integrations/next-js.ts` (+3 -3) </details> ### 📄 Description ## Summary - Adds `.js` extension to all three `import("next/headers")` calls in `nextCookies()` plugin - Bare specifier `"next/headers"` fails to resolve under Next.js 16's stricter ESM module resolution, especially when using bun as the runtime - This causes chunk resolution errors during production builds (`Failed to load chunk server/chunks/ssr/...`) ## Details Next.js 16 enforces proper ESM specifiers for dynamic imports. The bare `"next/headers"` works with Node.js resolution but fails with bun's runtime and Turbopack's stricter chunking. Adding the `.js` extension makes the import resolve correctly in all environments. **Before:** ```ts const { cookies } = await import("next/headers"); ``` **After:** ```ts const { cookies } = await import("next/headers.js"); ``` Fixes #6781 ## Test plan - [x] Verified fix resolves the build error in a Next.js 16 + bun monorepo - [ ] Existing tests should pass — no behavioral change, only import resolution <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Switch nextCookies to use import("next/headers.js") instead of import("next/headers") so Next.js 16 resolves the module correctly and production builds don’t fail with chunk loading errors, especially on bun/Turbopack. - **Bug Fixes** - Replace all dynamic imports of "next/headers" with "next/headers.js". - Resolves build-time “Failed to load chunk” errors in Next.js 16 with bun/Turbopack. - No behavior change; only fixes ESM import resolution. <sup>Written for commit dc8a1fbac05e32b7dd16e9454518eefe72535ac7. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:21:49 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16044