From 2629b67124bf1365e85720f690a902fc78abefca Mon Sep 17 00:00:00 2001 From: Jiahao Zhu Date: Tue, 7 Jul 2026 23:42:13 +0800 Subject: [PATCH] fix(aws): copy server/instrumentation.js into standalone dir on Next 16 Next.js 16's standalone output no longer includes server/instrumentation.js in the standalone directory. copyTracedFiles copies the instrumentation .nft.json trace and then asserts the .js exists in standaloneNextDir, throwing "File server/instrumentation.js does not exist" during the server bundle. Copy the instrumentation .js from the build dir into the standalone dir (mirroring the existing .nft.json copy) so the assertion passes and the file ships in the bundle. --- .changeset/next16-standalone-instrumentation.md | 13 +++++++++++++ packages/open-next/src/build/copyTracedFiles.ts | 14 ++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 .changeset/next16-standalone-instrumentation.md diff --git a/.changeset/next16-standalone-instrumentation.md b/.changeset/next16-standalone-instrumentation.md new file mode 100644 index 000000000..63643a5fd --- /dev/null +++ b/.changeset/next16-standalone-instrumentation.md @@ -0,0 +1,13 @@ +--- +"@opennextjs/aws": patch +--- + +Fix `server/instrumentation.js does not exist` build failure on Next.js 16 + +On Next.js 16 the standalone output no longer copies `server/instrumentation.js` +into the standalone directory, but `copyTracedFiles` copies the instrumentation +`.nft.json` trace and then asserts the `.js` file exists in the standalone dir, +throwing `File server/instrumentation.js does not exist` during the server +bundle. The instrumentation file is now copied from the build dir into the +standalone dir (mirroring the existing `.nft.json` copy) so the assertion passes +and the file ships in the bundle. diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 9f6689c0f..317006d04 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -243,6 +243,20 @@ File ${serverPath} does not exist path.join(dotNextDir, INSTRUMENTATION_TRACE_FILE), path.join(standaloneNextDir, INSTRUMENTATION_TRACE_FILE), ); + // Next 16's standalone output no longer includes `server/instrumentation.js` + // in the standalone dir, but `computeCopyFilesForPage` asserts it exists + // there. Copy it from the build dir (mirroring the .nft.json copy above) so + // the assertion passes and the instrumentation file ships in the bundle. + const instrumentationServerFile = path.join("server", "instrumentation.js"); + if ( + existsSync(path.join(dotNextDir, instrumentationServerFile)) && + !existsSync(path.join(standaloneNextDir, instrumentationServerFile)) + ) { + copyFileAndMakeOwnerWritable( + path.join(dotNextDir, instrumentationServerFile), + path.join(standaloneNextDir, instrumentationServerFile), + ); + } computeCopyFilesForPage("instrumentation"); logger.debug("Adding instrumentation trace files"); }