#!/usr/bin/env node /** * Runs before `next build`. Ensures `.next/` is writable and removes stale diagnostics. * * `build-diagnostics.json` only holds build metadata (stage, options) — failures are almost * always EACCES from root-owned `.next/` after `sudo npm run build`. * * Fix: sudo bash scripts/fix-next-perms.sh * or: sudo chown -R "$(whoami)" .next * or: sudo rm -rf .next */ "use strict"; const fs = require("fs"); const path = require("path"); const repoRoot = path.join(__dirname, ".."); const nextDir = path.join(repoRoot, ".next"); const diagnostics = path.join(nextDir, "diagnostics"); const diagFile = path.join(diagnostics, "build-diagnostics.json"); function die(msg) { console.error(msg); process.exit(1); } if (fs.existsSync(nextDir)) { try { fs.accessSync(nextDir, fs.constants.W_OK); } catch { die(` [!] ${nextDir} is not writable (often root-owned). Next.js cannot update .next/diagnostics/build-diagnostics.json. Fix (one of): sudo chown -R "$(whoami)" "${nextDir}" sudo bash scripts/fix-next-perms.sh sudo rm -rf "${nextDir}" `); } } if (fs.existsSync(diagFile)) { try { fs.accessSync(diagFile, fs.constants.W_OK); } catch { die(` [!] ${diagFile} is not writable. Same fix as above — give your user ownership of .next/ sudo chown -R "$(whoami)" "${nextDir}" `); } } try { fs.rmSync(diagnostics, { recursive: true, force: true }); } catch (e) { if (e && (e.code === "EACCES" || e.code === "EPERM")) { die(` [!] Cannot remove ${diagnostics} ${e.message} sudo chown -R "$(whoami)" "${nextDir}" `); } if (e && e.code !== "ENOENT") throw e; }