next.js: Building production when using localhost:3000 in a fetch gives ECONNREFUSED
Link to the code that reproduces this issue
https://github.com/douwepausma/build-with-routehandlers-reproduction
To Reproduce
- Create new next-app
npx create-next-app@latest
- Create a simple route.
// /app/api/model/route.js
export async function GET() {
return Response.json({ data: {message: 'Hello World!'} })
}
- Fetch route from page.
// /app/page.js
export default async function Home() {
try {
const res = await fetch('http://localhost:3000/api/model');
} catch (error) {
console.log(error);
}
return null;
}
npm run build
Current vs. Expected behavior
Current behavior
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11372:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: Error: connect ECONNREFUSED ::1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3000
}
}
Expected behavior Successful build
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:43:05 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6020
Binaries:
Node: 18.18.2
npm: 9.8.1
Yarn: 1.22.19
pnpm: 8.11.0
Relevant Packages:
next: 14.0.4-canary.32
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: N/A
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Data fetching (gS(S)P, getInitialProps), Middleware / Edge (API routes, runtime)
Additional context
What I’ve tried
- Use
127.0.0.1
instead oflocalhost
- Adding
export const runtime = 'edge'
to page fixed build issue, but breaks app later on (when deploying on vercel).* - Adding
const dynamic = 'force-dynamic'
to page fixed build issue, but breaks app later on (when deploying on vercel).*
*These fixes shouldn’t be necessary as I’ve successfully used route handlers in the past without it.
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 18 (1 by maintainers)
I am also getting this error running in gitlab, my gitlab runner is hosted on my Mac Mini with an M2 Pro chip.
Running
npm run dev
on my Mac mini first and then executing my pipeline (which will runnpm run build
) seemed to work and resolved the issue, and my static HTML does appear to have the contents from that route fetch. All of this without using client components.The
export const dynamic = 'force-dynamic'
doesn’t appear to build the same static HTML, which I’d expect since it’s a dynamic page.I’ll probably stick with the Dynamic page for now.
Thanks for the couple solutions, would be cool to somehow get static pages built out using the
run dev
run build
in tandem.Hello @douwepausma, could you please try adding
export const dynamic = 'force-dynamic'
toapp/page.tsx
?And another test with this one https://nextjs.org/docs/app/api-reference/functions/unstable_noStore, add
noStore();
Another test i would like to confirm, run
npm run dev
in a terminal, andnpm run build
in another one.There is something with the static rendering & ARM. Thanks for your time.
i hame same error, on macbook m1