next/server Build Issue

Hi, I'm running into an issue during my builds using Nextjs on Pages. This seems rather strange since the example on the next-on-pages repo shows next/server being used in middleware. (https://github.com/cloudflare/next-on-pages#middleware) Thanks for any pointers/suggestions! My next.config.js is of course configured to include experimental: {runtime: "experimental-edge",} And I've included the log and my middleware below. Latest deployment ID is: 5439b547e0894d3022943819c304bcc9
GitHub
GitHub - cloudflare/next-on-pages: CLI to build and develop Next.js...
CLI to build and develop Next.js apps for Cloudflare Pages - GitHub - cloudflare/next-on-pages: CLI to build and develop Next.js apps for Cloudflare Pages
9 Replies
Mackenly
Mackenly15mo ago
16:23:50.927 ▲ Build Completed in .vercel/output [31s] 16:23:50.927 ▲ 16:23:51.080 ⚡️ 16:23:51.081 ⚡️ 16:23:51.081 ⚡️ Completed 'npx vercel build'. 16:23:51.081 ⚡️ 16:23:51.429 ✘ [ERROR] Could not resolve "next/server" 16:23:51.429 16:23:51.430 ../../../tmp/iy1gcmczsgf/middleware.func.js:1:27: 16:23:51.430 1 │ import {NextResponse} from 'next/server'; 16:23:51.430 ╵ ~~~~~~~~~~~~~ 16:23:51.430 16:23:51.431 You can mark the path "next/server" as external to exclude it from the bundle, which will remove this error. 16:23:51.431 16:23:51.468 /opt/buildhome/.npm/_npx/39e2fd552b408994/node_modules/esbuild/lib/main.js:1575 16:23:51.468 let error = new Error(${text}${summary}); 16:23:51.469 ^ 16:23:51.469 This is for super basic username/password protection. import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; export const config = { matcher: ['/', '/index'], } export function middleware(request: NextRequest) { const basicAuth = request.headers.get('authorization') const url = request.nextUrl if (basicAuth) { const authValue = basicAuth.split(' ')[1] const [user, pwd] = atob(authValue).split(':') if (user === process.env.NEXT_PUBLIC_USERNAME && pwd === process.env.NEXT_PUBLIC_PASSWORD) { return NextResponse.next() } } url.pathname = '/api/auth' return NextResponse.rewrite(url) }
James
James15mo ago
Can you open an issue on the repository for this, please? If possible, please try to include a minimal reproduction. I have a theory as to why this might occur, but I'm unable to test and experiment with it at the moment.
Mackenly
Mackenly15mo ago
I've had a bit of trouble because the next starter I used to get going used pnpm but according to this issue Pages is a bit iffy when it comes to pnpm. https://github.com/cloudflare/next-on-pages/issues/52 From my understanding I have implemented the fixes proposed in that issue. Here's a copy of my package.json which * should * by pulling in next including next/server { "name": "basic-auth-password", "repository": "https://github.com/vercel/examples.git", "license": "MIT", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "next": "canary", "pnpm": "^7.30.3", "react": "latest", "react-dom": "latest" }, "devDependencies": { "@types/node": "^17.0.45", "@types/react": "latest", "autoprefixer": "^10.4.14", "eslint": "^8.36.0", "eslint-config-next": "canary", "postcss": "^8.4.21", "tailwindcss": "^3.2.7", "typescript": "4.5.5" } }
GitHub
Error: spawn npm ENOENT · Issue #52 · cloudflare/next-on-pages
When trying to run npx @cloudflare/next-on-pages I get an error C:\Users......\project>npx @cloudflare/next-on-pages @cloudflare/next-to-pages CLI ⚡️ ⚡️ Installing 'vercel' CLI... ⚡️ n...
Mackenly
Mackenly15mo ago
Will do. Funny enough it works on my machine but evidently either a bug or something misconfigured on my end causing build issues on Pages.
James
James15mo ago
One would think that, however I believe when I was trying some things a few days ago that middleware is actually built with its own node_modules dependencies in the build output (IIRC) And that looks like an error from when esbuild is building the worker script, suggesting that it's having a trouble with the dependencies that the built middleware file needs from the build output I'm on my laptop for the next couple weeks, and my next-on-pages dev setup is on my desktop 🙄
James
James15mo ago
James
James15mo ago
Yeah, this is what I'm referring to middleware are the only part that build an output like this with the vercel build output And I think that may be why
Mackenly
Mackenly15mo ago
Issue created: https://github.com/cloudflare/next-on-pages/issues/130 Thanks for looking at this yall!
GitHub
next/server Build Issue · Issue #130 · cloudflare/next-on-pages
Creating this issue to follow up on a post made in Discord. Hi, I'm running into an issue during my builds using Nextjs on Pages. This seems rather strange since the example on the next-on-page...
James
James15mo ago
Thanks There are a few oddities with certain parts of the Vercel build output, which aren't helped by the lack of documentation around it, but it's possible this could be one of those situations. I haven't tested or looked into middleware personally, and I'm not sure if anyone on the Cloudflare team assigned to the repo has, although I'll certainly take a look when building the new routing solution I've been designing. It's an interesting one for sure