{"record":{"id":"215454e340fd4b19","repo":"vercel/next.js","slug":"next-export-error","errorCode":"NEXT_EXPORT_ERROR","errorMessage":"Could not find a production build in the '${distDir}' directory. Try building your app with 'next build' before starting the static export. https://nextjs.org/docs/messages/next-export-no-build-id","messagePattern":"Could not find a production build in the '(.+?)' directory\\. Try building your app with 'next build' before starting the static export\\. https://nextjs\\.org/docs/messages/next-export-no-build-id","errorType":"exception","errorClass":"ExportError","httpStatus":null,"severity":"critical","filePath":"packages/next/src/export/index.ts","lineNumber":240,"sourceCode":"        hasNowJson: !!(await findUp('now.json', { cwd: dir })),\n        isCustomServer: null,\n        turboFlag: options.bundler === Bundler.Turbopack,\n        pagesDir: null,\n        appDir: null,\n      })\n    )\n  }\n\n  const subFolders = nextConfig.trailingSlash && !options.buildExport\n\n  if (!options.silent && !options.buildExport) {\n    Log.info(`using build directory: ${distDir}`)\n  }\n\n  const buildIdFile = join(distDir, BUILD_ID_FILE)\n\n  if (!existsSync(buildIdFile)) {\n    throw new ExportError(\n      `Could not find a production build in the '${distDir}' directory. Try building your app with 'next build' before starting the static export. https://nextjs.org/docs/messages/next-export-no-build-id`\n    )\n  }\n\n  const customRoutes = (['rewrites', 'redirects', 'headers'] as const).filter(\n    (config) => typeof nextConfig[config] === 'function'\n  )\n\n  if (!hasNextSupport && !options.buildExport && customRoutes.length > 0) {\n    Log.warn(\n      `rewrites, redirects, and headers are not applied when exporting your application, detected (${customRoutes.join(\n        ', '\n      )}). See more info here: https://nextjs.org/docs/messages/export-no-custom-routes`\n    )\n  }\n\n  const buildId = await fs.readFile(buildIdFile, 'utf8')\n","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/vercel/next.js/blob/0ae8c72462952df163f1b1e0726641bc5b40dc93/packages/next/src/export/index.ts#L222-L258","documentation":"At export/index.ts:237-243 the static exporter checks for `${distDir}/BUILD_ID`. If the file does not exist, it throws this `ExportError` (`code: NEXT_EXPORT_ERROR`). The export step (`next export`) operates on a completed production build, so a missing BUILD_ID means `next build` was never run (or ran into a different `distDir`).","triggerScenarios":"Running `next export` without a prior successful `next build`; the build wrote to a different `distDir` than export reads; the `.next` directory was deleted or not deployed; `distDir` was customized in config but the export runs against the default.","commonSituations":"CI pipeline that runs `next export` without `next build`; a clean step that wiped `.next`; renaming `distDir` in config; running export in a fresh checkout.","solutions":["Run `next build` before `next export` (or use `output: 'export'` in config so build produces the export directly).","Confirm `distDir` in next.config.js matches the directory export is reading.","Ensure the build step succeeded and `.next/BUILD_ID` exists before invoking export."],"exampleFix":"// before — running export without build\nnext export\n\n// after — build first, or use output: 'export'\nnext build && next export\n// OR next.config.js\nmodule.exports = { output: 'export' } // `next build` then emits static output","handlingStrategy":"validation","validationCode":"const fs = require('fs')\nconst path = require('path')\nconst distDir = '.next' // or read from config\nif (!fs.existsSync(path.join(distDir, 'BUILD_ID'))) {\n  throw new Error('Run `next build` before exporting')\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run next build before next export, or use output: 'export' to do both in one step.","Ensure CI pipelines run build then export in order.","Confirm distDir in next.config.js is consistent across build and export."],"tags":["static-export","build","config","next-export"],"analyzedSha":"0ae8c72462952df163f1b1e0726641bc5b40dc93","analyzedAt":"2026-08-06T19:44:29.143Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}