Skip to content

[backport] Fix "type: module" in project dir when using standalone or adapters#94050

Merged
timneutkens merged 5 commits into
next-16-2from
codex/backport-93612-to-next-16-2
May 28, 2026
Merged

[backport] Fix "type: module" in project dir when using standalone or adapters#94050
timneutkens merged 5 commits into
next-16-2from
codex/backport-93612-to-next-16-2

Conversation

@timneutkens

Copy link
Copy Markdown
Contributor

…93612)

### What?

Add `.next/package.json` (the distDir commonjs boundary marker) to the
`required-server-files.json` manifest.

### Why?

`next build` writes `.next/package.json` with `{"type": "commonjs"}` so
that everything in `.next/**` is loaded as CJS even when the user's
project `package.json` has `"type": "module"`. Without that file, Node
walks up to the project `package.json` when loading
`.next/server/**/*.js` and tries to evaluate the compiled server bundles
as ESM, which fails at runtime.

This file is necessary on disk in every server deployment artifact (the
standalone bundle, every adapter Node function), but it was not declared
in `required-server-files.json`. As a result, adapters consuming
`requiredServerFiles.files` did not include the boundary file.

`.next/package.json` is conceptually a required server file: any
deployment that wants to run `.next/server/**/*.js` correctly under a
`"type": "module"` project must ship it. Declaring it in the manifest is
the right place to express that contract once for every consumer.

This error case would only happen if the project package.json gets
included into the Node.js functions. For example when file tracing
analysis (node-file-trace) decides it needs to include it (i.e. glob
patterns or such).

### How?

Add `'package.json'` to the `requiredServerFilesManifest.files` array in
`packages/next/src/build/index.ts`. Every entry in that array is joined
with `config.distDir`, so this resolves to `.next/package.json`.
Both existing consumers pick it up automatically with no further
changes:
- `packages/next/src/build/adapter/build-complete.ts` iterates
`requiredServerFiles` into `sharedNodeAssets`, which is merged into
every Node function's `output.assets` regardless of bundler. The Vercel
adapter (and any other adapter) gets the file for free.
- `copyTracedFiles` in `packages/next/src/build/utils.ts` iterates
`requiredServerFiles.files` and copies each entry into
`.next/standalone/<relative>`, so the standalone output now contains
`.next/standalone/.next/package.json`.

### Tests
- `test/production/required-server-files-package-boundary/` — a focused,
non-standalone test that builds a `"type": "module"` app and asserts:
  - `.next/package.json` is `{"type": "commonjs"}` on disk
  - `.next/required-server-files.json` lists `.next/package.json`
- `test/production/standalone-mode/type-module/` — extended with a
`pages/dynamic.js` that uses `getServerSideProps`. The test now:
- Asserts `.next/standalone/.next/package.json` exists with `{"type":
"commonjs"}`
- Boots the standalone `server.js` and fetches `/dynamic`, which forces
Node to actually evaluate `.next/server/pages/dynamic.js` at runtime —
the exact code path the boundary file makes work

(cherry picked from commit 8f132ea)
@github-actions

github-actions Bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 27b04ea

@timneutkens timneutkens marked this pull request as ready for review May 22, 2026 18:11
@timneutkens timneutkens requested a review from mischnic May 22, 2026 18:12
@github-actions

github-actions Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Stats skipped

Commit: 27b04ea
View workflow run

@timneutkens timneutkens merged commit f126e72 into next-16-2 May 28, 2026
137 of 140 checks passed
@timneutkens timneutkens deleted the codex/backport-93612-to-next-16-2 branch May 28, 2026 15:23
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