Deploy to Cloudflare Pages
Deploying a Nextra 3 site to Cloudflare Pages is powerful but requires specific settings, especially when developing on macOS (Apple Silicon). This guide covers the complete setup and how to avoid cross-platform errors.
1. Project Configuration
Static Export
Open next.config.js and ensure you have enabled static export. This is the most efficient way to host documentation.
const withNextra = require('nextra').default({
theme: 'nextra-theme-docs',
themeConfig: './theme.config.tsx',
})
module.exports = withNextra({
output: 'export', // Required for Static Site Generation
images: {
unoptimized: true, // Required for static export
},
})⚠️ Cross-Platform Warning (macOS vs Linux)
Nextra relies on native binaries (like @napi-rs/simple-git).
- Local (macOS): Uses
darwin-arm64binary. - Cloudflare (Linux): Needs
linux-x64binary.
If you mistakenly commit package-lock.json generated on macOS, Cloudflare’s build may fail with EBADPLATFORM because it tries to install the Mac binary on Linux.
Recommendation:
To avoid this, use npm install instead of npm ci in your build command, or do not commit package-lock.json. The instructions below use the npm install method which is safest.
2. Push to GitHub
Ensure your code is pushed to your remote repository.
git add .
git commit -m "Ready for deployment"
git push origin main3. Cloudflare Pages Setup (Detailed)
- Log in to the Cloudflare Dashboard.
- Go to Workers & Pages > Create Application > Pages tab > Connect to Git.
- Select your repository (e.g.,
the-whole-scraps).
Build Settings Configuration
On the “Set up builds and deployments” screen, configure as follows:
- Project Name:
my-nextra3(or your preference) - Production Branch:
main - Framework Preset: Select Next.js (Static HTML Export).
- Build Output Directory:
out(Automatically set by preset)
⚠️ Critical Settings
-
Build Command: Change from
npx next buildto:npm install && npm run buildWhy? The standard
npm ci(used by default) is strict about lockfiles. Usingnpm installensures Cloudflare resolves the correct Linux binaries freshly, ignoring any macOS-specific locks in your repository. -
Root Directory:
- Expand the Build configurations section.
- If your Nextra app is in a specific folder (e.g.,
cloudflarepages-nextra/my-nextra3), you MUST enter that path here. - Example:
cloudflarepages-nextra/my-nextra3
-
Environment Variables (Optional):
- Add
NODE_VERSION:18(or20) if you want to be explicit, though Cloudflare usually picks a recent version.
- Add
4. Deploy
Click Save and Deploy.
Cloudflare will:
- Clone your repository.
- Enter the Root Directory you specified.
- Run
npm install(resolving Linux dependencies). - Run
npm run build(generating theoutfolder). - Deploy the
outfolder to the edge.
5. Automatic Deployments
Once set up, every time you run git push origin main, Cloudflare will detect the change and automatically trigger a new deployment. You can verify this setting in Settings > Builds & deployments.