Post build script cannot access a NextJS build output file

I have a Nextjs project that I am trying to deploy.

My build script is:

"scripts": {
    "build": "next build",
    "postbuild": "pnpm run build:post",
}

In the post build script I am trying to access index.html build output file at .next/server/app however the file is not accessible at the expected location.

In my local development environment the build output file is accessible from the post build script.

A static route at app/static.json/route.ts is trying to access the build file. So maybe the build file is unavailable when the static route is being built?

But I don't understand why it works locally, even after clearing .next folder

I have attached the build log below

```text
[20:14:49.435] Cloning github.com/dcs3spp/my-fumadocs-app (Branch: main, Commit: 6e200d5)
[20:14:50.065] Cloning completed: 630.000ms
[20:14:55.986] Restored build cache from previous deployment (AwbHpuZQzW6yH2LTiYyoYtwMyN1S)
[20:14:56.099] Running build in Washington, D.C., USA (East) – iad1
[20:14:57.203] Running "vercel build"
[20:14:57.674] Vercel CLI 41.4.1
[20:14:58.226] Detected `pnpm-lock.yaml` 9 which may be generated by pnpm@9.x or pnpm@10.x
[20:14:58.226] Using pnpm@10.x based on project creation date
[20:14:58.227] To use pnpm@9.x, manually opt in using corepack (https://vercel.com/docs/deployments/configure-a-build#corepack)
[20:14:58.260] Installing dependencies...
[20:15:01.558] Lockfile is up to date, resolution step is skipped
[20:15:01.801] Already up to date
[20:15:02.707] 
[20:15:02.728] Done in 2.2s using pnpm v10.6.3
[20:15:02.745] Detected Next.js version: 15.2.2
[20:15:02.768] Running "pnpm run build"
[20:15:03.150] 
[20:15:03.150] > my-fumadocs-app@0.0.0 build /vercel/path0
[20:15:03.151] > next build
[20:15:03.151] 
[20:15:04.293]    ▲ Next.js 15.2.2
[20:15:04.293] 
[20:15:04.866]    Creating an optimized production build ...
[20:15:05.695] [MDX] update map file: 664.962ms
[20:15:40.099]  ✓ Compiled successfully
[20:15:40.145]    Skipping linting
[20:15:40.146]    Checking validity of types ...
[20:15:49.481]    Collecting page data ...
[20:15:59.050]    Generating static pages (0/14) ...
[20:16:00.920]    Generating static pages (3/14) 
[20:16:00.920]    Generating static pages (6/14) 
[20:16:00.927] Error extracting home page content: Error: ENOENT: no such file or directory, open '.next/server/app/index.html'
[20:16:00.927]     at async nI (.next/server/app/static.json/route.js:10:191093)
[20:16:00.928]     at async nd (.next/server/app/static.json/route.js:10:191708) {
[20:16:00.928]   errno: -2,
[20:16:00.928]   code: 'ENOENT',
[20:16:00.929]   syscall: 'open',
[20:16:00.929]   path: '.next/server/app/index.html'
[20:16:00.929] }
[20:16:01.778]    Generating static pages (10/14) 
[20:16:01.779]  ✓ Generating static pages (14/14)
[20:16:02.162]    Finalizing page optimization ...
[20:16:02.167]    Collecting build traces ...
[20:16:20.836] 
[20:16:20.841] Route (app)                                                        Size  First Load JS
[20:16:20.842] ┌ ○ /                                                           40.8 kB         149 kB
[20:16:20.842] ├ ○ /_not-found                                                   985 B         103 kB
[20:16:20.842] ├ ƒ /api/search/docs                                              535 B         127 kB
[20:16:20.843] ├ ƒ /api/search/posts                                             535 B         127 kB
[20:16:20.843] ├ ● /docs/[[...slug]]                                             581 B         147 kB
[20:16:20.843] ├   ├ /docs/examples/demos
[20:16:20.843] ├   └ /docs/examples/ui
[20:16:20.843] ├ ● /posts/[[...slug]]                                          6.45 kB         153 kB
[20:16:20.844] ├   ├ /posts
[20:16:20.844] ├   ├ /posts/realtime/web-notification/part-1-architecture
[20:16:20.844] ├   ├ /posts/realtime/web-notification/part-2-pubsubhubbub
[20:16:20.844] ├   └ /posts/realtime/web-notification/part-3-pubsub-push-auth
[20:16:20.844] ├ ○ /sitemap.xml                                                  141 B         103 kB
[20:16:20.844] └ ƒ /static.json                                                  535 B         127 kB
[20:16:20.844] + First Load JS shared by all                                    102 kB
[20:16:20.844]   ├ chunks/609eec2e-7001bd2fe56755fe.js                         53.3 kB
[20:16:20.845]   ├ chunks/9077-e48bf21ea1184d6a.js                             46.3 kB
[20:16:20.845]   └ other shared chunks (total)                                 2.83 kB
[20:16:20.845] 
[20:16:20.845] 
[20:16:20.845] ○  (Static)   prerendered as static content
[20:16:20.845] ●  (SSG)      prerendered as static HTML (uses generateStaticParams)
[20:16:20.845] ƒ  (Dynamic)  server-rendered on demand
[20:16:20.845] 
[20:16:20.948] 
[20:16:20.948] > my-fumadocs-app@0.0.0 postbuild /vercel/path0
[20:16:20.955] > pnpm run build:post
[20:16:20.958] 
[20:16:21.359] 
[20:16:21.360] > my-fumadocs-app@0.0.0 build:post /vercel/path0
[20:16:21.361] > tsx ./scripts/post-build.mts
[20:16:21.361] 
[20:16:21.877] 🗂 Files in .next/server/app/ [
[20:16:21.877]   '(home)',           '_not-found',
[20:16:21.878]   '_not-found.html',  '_not-found.meta',
[20:16:21.878]   '_not-found.rsc',   'api',
[20:16:21.878]   'docs',             'index.html',
[20:16:21.879]   'index.meta',       'index.rsc',
[20:16:21.879]   'posts',            'posts.html',
[20:16:21.879]   'posts.meta',       'posts.rsc',
[20:16:21.880]   'sitemap.xml',      'sitemap.xml.body',
[20:16:21.880]   'sitemap.xml.meta', 'static.json'
[20:16:21.880] ]
[20:16:21.881] 🗂 Files in .next/static/ [ 'NqanZ9R5E7IuwOJlcCpj_', 'chunks', 'css', 'media' ]
[20:16:21.881] 🔑 Using Orama Index: <my index>
[20:16:21.881] 🐞 Debug Mode: OFF (syncing to cloud)
[20:16:21.883] ❌ Failed to update search indexes: Error: ENOENT: no such file or directory, open '.next/server/app/static.json.body'
[20:16:21.887]     at async open (node:internal/fs/promises:638:25)
[20:16:21.887]     at async Module.readFile (node:internal/fs/promises:1242:14)
[20:16:21.888]     at async updateSearchIndexes (file:///vercel/path0/scripts/update-search-index.mjs:39:21)
[20:16:21.888]     at async Promise.all (index 0)
[20:16:21.889]     at async main (/vercel/path0/scripts/post-build.mts:19:3)
[20:16:21.889]     at async <anonymous> (/vercel/path0/scripts/post-build.mts:22:1) {
[20:16:21.889]   errno: -2,
[20:16:21.889]   code: 'ENOENT',
[20:16:21.889]   syscall: 'open',
[20:16:21.890]   path: '.next/server/app/static.json.body'
[20:16:21.890] }
[20:16:22.102] Traced Next.js server files in: 132.206ms
[20:16:22.617] Created all serverless functions in: 516.118ms
[20:16:22.640] Collected static files (public/, static/, .next/static): 17.496ms
[20:16:22.829] Build Completed in /vercel/output [1m]
[20:16:23.097] Deploying outputs...
[20:16:32.541] 
[20:16:32.804] Deployment completed

Never mind, solved it. It was because the route was being built and was trying to access a build output file before it was built.

Moved accessing the build artefact into the post build script and deploy succeeded.

Issue can be closed

Thanks for coming back to share the solution!