{"record":{"id":"284b53cfbc84cad8","repo":"payloadcms/payload","slug":"could-not-find-exportdefaultdeclaration-in-next-co","errorCode":null,"errorMessage":"Could not find ExportDefaultDeclaration in next.config.js","messagePattern":"Could not find ExportDefaultDeclaration in next\\.config\\.js","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/create-payload-app/src/lib/wrap-next-config.ts","lineNumber":85,"sourceCode":"          )\n          return { modifiedConfigContent, success: true }\n        }\n\n        return Promise.resolve({\n          modifiedConfigContent: content,\n          success: false,\n        })\n      } else if (configType === 'esm') {\n        const exportDefaultDeclaration = ast.body.find(\n          (p) => p.type === Syntax.ExportDefaultDeclaration,\n        ) as Directive | undefined\n\n        const exportNamedDeclaration = ast.body.find(\n          (p) => p.type === Syntax.ExportNamedDeclaration,\n        ) as ExportNamedDeclaration | undefined\n\n        if (!exportDefaultDeclaration && !exportNamedDeclaration) {\n          throw new Error('Could not find ExportDefaultDeclaration in next.config.js')\n        }\n\n        if (exportDefaultDeclaration && exportDefaultDeclaration.declaration?.loc) {\n          const modifiedConfigContent = insertBeforeAndAfter(\n            content,\n            exportDefaultDeclaration.declaration.loc,\n          )\n          return { modifiedConfigContent, success: true }\n        } else if (exportNamedDeclaration) {\n          const exportSpecifier = exportNamedDeclaration.specifiers.find(\n            (s) =>\n              s.type === 'ExportSpecifier' &&\n              s.exported?.name === 'default' &&\n              s.local?.type === 'Identifier' &&\n              s.local?.name,\n          )\n\n          if (exportSpecifier) {","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/create-payload-app/src/lib/wrap-next-config.ts#L67-L103","documentation":"While parsing an ESM next.config.js, esprima-next found neither an ExportDefaultDeclaration nor an ExportNamedDeclaration in the AST body, so there is no export to wrap with withPayload. The message names only ExportDefaultDeclaration but the guard also covers named exports.","triggerScenarios":"create-payload-app init/upgrade on a Next.js project whose next.config.js (detected as ESM) has no export default or export statement — e.g. a config file that only assigns globals, re-exports via dynamic import, or was left as a stub.","commonSituations":"A next.config.js that exports nothing (side-effect only); a config already using a non-standard pattern; accidental CJS syntax in a .js file treated as ESM; a manually emptied config.","solutions":["Open next.config.js and ensure it has `export default { ... }` (ESM) so withPayload can wrap it.","If the project actually uses CommonJS, ensure the file/extension detection picks the cjs path (rename to next.config.cjs or set type accordingly).","As a fallback, follow the printed instructions to manually wrap the config with withPayload.","Run the CLI on a clean, standard Next.js config first to confirm, then port your customizations."],"exampleFix":"// before — next.config.js with no export\nconsole.log('config loading')\n\n// after\nimport { withPayload } from '@payloadcms/next/withPayload'\n\nconst nextConfig = {\n  // your config\n}\n\nexport default withPayload(nextConfig)","handlingStrategy":"validation","validationCode":"import fs from 'fs'\n\nfunction assertNextConfigHasExport(configPath: string) {\n  const src = fs.readFileSync(configPath, 'utf8')\n  if (!/export\\s+default\\b/.test(src) && !/export\\s+\\{/.test(src)) {\n    throw new Error(`${configPath} has no export default or named export — cannot wrap with withPayload`)\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await wrapNextConfig({ nextConfigPath, nextConfigType: 'esm' })\n} catch (e) {\n  if (e instanceof Error && e.message.includes('ExportDefaultDeclaration')) {\n    console.error('Manually wrap your next.config.js export with withPayload (see printed instructions).')\n  }\n  throw e\n}","preventionTips":["Keep next.config.js as a standard `export default { ... }`.","If using CommonJS, name the file next.config.cjs so detection picks the cjs branch.","Run init on a clean standard config first, then port customizations."],"tags":["create-payload-app","nextjs","ast","config","esm"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}