AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack: "Type": "Commonjs" In Package.Json Breaks Every App Router Source File

Setting `"type": "commonjs"` in a Next.js project using Turbopack causes build and dev failures because Turbopack enforces the declared module format on App Router source files (which are written as ESM) and on generated `.next-internal` server-action entries, even though Webpack ignores this field. The issue affects all App Router files and internal virtual modules, not just the `'use client'` proxy previously fixed.

highConfidence 68%Next.jsAffected V15.5.26Affected V16.3.5Affected V16.3.6Affected V16.4.0-Canary.43

Origin Analysis

Turbopack honors the explicit `package.json` `"type": "commonjs"` and refuses to bundle ESM syntax under that mode. Next.js's Turbopack integration does not override the module format for App Router sources (which are always ESM), and it lacks a module-format boundary for the generated `.next-internal` directory, causing those virtual ESM files to inherit the project's CommonJS type.
1. `pnpm dlx create-next-app@latest repro --ts --app --turbopack` 2. Add `"type": "commonjs"` to `package.json` 3. Run `pnpm exec next build` (or `next dev`) → build fails with 'Specified module format (CommonJs) is not matching the module format of the source code (EcmaScript Modules)' for `app/layout.tsx` and `app/page.tsx`; server-action generated files under `.next-internal` also fail.

Fixing Code Block

Edge Case Audit

This hotfix forces ESM for all App Router sources; any legitimate CommonJS module inside `app/` (using `require` or `module.exports`) will break. The `.next-internal/package.json` write may conflict with existing generated artifacts; a clean rebuild is recommended. Rolling back this patch and using Webpack (`next build --webpack`) or temporarily removing `"type": "commonjs"` are safe fallbacks. Test thoroughly in monorepo setups where multiple packages may rely on the project-level `type` field, and verify server actions and client proxies still resolve correctly.

Ecosystem Topology