next.js: Next.js dev server gets stuck after a while, requests just spin

Bug report

Describe the bug

When I run my Next.js app with now dev, after a while (on the order of minutes, definitely less than an hour) my requests to it will stop working.

To Reproduce

I’m not sure how to isolate this one. It happens all the time with this project https://github.com/mjm/courier-js but it’s not an easy thing to get running for reproduction.

  1. Run now dev
  2. Work on the app, making changes to files, saving, and watching them hot reload in the browser
  3. Eventually, one of these changes will try to reload and will get stuck. The on-page indicator for a page compiling will be there and keep spinning continuously. Trying to refresh the page just causes the browser to try to load the page until it times out.

When I give up, I can hit Ctrl-C to try to kill the server, but if it’s in this state, it can take a minute or two to actually get the message and quit.

Expected behavior

Continuing to make changes should keep working and trigger refreshes in the browser.

System information

  • OS: macOS Catalina, though I think this happened before upgrading too
  • Browser (if applies): I’ve seen in both Chrome and Safari
  • Version of Next.js: 9.1.7, has happened in earlier versions of Next 9 as well
  • Version of Now CLI: 16.7.3

Additional context

I feel like this could be TypeScript related, but I don’t have any evidence. I’m on the latest release for that as well. I’m happy to try things that might help understand what’s going on. The only thing I’ve seen in the log from now dev (or next dev I guess) is that it seems like when it gets in this state, it will stack up two or three “compiling …” messages when there would otherwise be just one.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 125
  • Comments: 141 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I ran into this problem as well, and it was caused by an old cached service worker. I cleaned the cache completely (under dev tools -> application in Chrome) and the problem was fixed. Not sure if your problem is due to the same cause.

Hi, I have the exact same issue, even in the clean Next.js starter app (npm init next-app, no additional dependencies)

To Reproduce

Run npm run dev or next dev and then refresh the page few times (hit CMD+R quickly, at least 10 or more times) the dev server gets stuck (requests have a pending status)

System information

  • OS: macOS Catalina, Version 10.15.4
  • Version of Next.js: 9.4.2, I had the same problem in previous versions of Next.js
  • All dependencies:
"dependencies": {
  "next": "9.4.2",
  "react": "16.13.1",
  "react-dom": "16.13.1"
}

Solution

I am not sure, but it seems to work in my case. When I start server with different port (not the default 3000) it works like a charm next dev -p 1234

this issue still exists on next13

I’ve figured out a way to reproduce this a bit, will try to solve the issue.

For anyone struggling with this in 2023. Updating node to v18 solved the problem for me on Mac Pro m1 / Ventura

I’ve been dealing with this issue for a long time. Latest nextjs, fresh install of macOS 10.15.4, it happens all the time, it’s a PITA.

The only thing that worked for me was @neupauer’s solution, running dev server on another port (in my case npx next -p 1234 works).

🎉

Likewise, same issue - seems intermittent and not tied to any particular change. It will work for a while, and then hang on the next change. Rerunning npm run dev again fixes it for a time, but it’s a little irritating to have to continually reload.

next dev doesn’t fix it for (same issue).

(FWIW I’m not using TS, just plain old JS)

@pi0 I have this issue on FF as well. In my case at least, once it happens, it just fails across all of the other browsers that I have i.e. Chrome, Edge and Safari.

I also know this only happens once there is a change in the source code.

I have also had this problem with using ‘vercel dev’ only for the API; i.e. I had my dev server separately setup for serving the front end and then vercel for the API and ended up in a situation where front end was working but API was stuck 🤷‍♂️

any advancements on this issue?

I think this problem is due to the chrome extension so check your browser i’m using chrome 89.0.4389.114 uninstall chrome extension all (do not disable, remove)

I had the same problem too. I have fixed this problem by remove service worker. When you change the development server port if the problem gets solved. The problem occurs because of the old service worker.

For Chrome. Open this page: chrome://serviceworker-internals and find localhost service workers and click to unregister button.

If you want to unregister all service worker open the developer tools and run this code on above page.

document.querySelectorAll("button.unregister").forEach(item=>{ item.click()})

👋 Hey folks. Wanted to summarize this issue.

Some of you all are experiencing an issue where the Next.js development server gets stuck after a period of time, causing requests to fail or time out. It’s tricky because the problem seems to occur intermittently and isn’t clearly tied to any particular change or action. There’s been reports across different operating systems and versions of Next.js.

Solutions / workarounds suggested throughout this issue:

  1. Running the app with just Next.js next dev instead of vercel dev (we recommend doing this regardless)
  2. Changing the port from the default 3000 to something else (e.g., running next dev -p 1234)
  3. Clearing the cache, including service workers
  4. Upgrading Node.js to version 18 or later
  5. Deleting the .next folder and node_modules and then rebuilding the project
  6. Having two instances of Next.js running simultaneously, terminating the extra process resolved the issue

Note that not everyone has been able to resolve the issue, and there isn’t a one-size-fits-all solution that works in all cases. I encourage everyone to keep sharing their experiences and any potential fixes they come across. And if you are still seeing an issue and can consistently reproduce, please post that reproduction so we can investigate. Thanks!

If you recently added a package, do checkout by removing it first.

Sheeesh, @next/font was the culprit in my case 🤦‍♀️

Guys, you won’t believe me, but I’ve just changed the port from localhost:3000 to localhost:4000 and the issue disappeared. I’m using next dev.

Magic ✨

Hi 😃 Just to summarize there was/are 3 well-known issues which might be shared:

Still, as there is no proper way to reproduce these were just some clues we found. It might be also a generic chrome bug.

This issue was opened like a year ago… how is it still busted lol.

here’s some debugging help:

  • i’ve tried this on hella node versions (all over 15, 16, 17) node ain’t the problem
$ node -e 'console.log(process.version, process.arch, process.platform)'
v17.2.0 arm64 darwin

next version:

 $ npx next -v
Next.js v12.0.7
  1. it ain’t my browser — it’s frozen when i curl localhost:3000

so if it’s not my browser, it must be my code right? well, no.

let’s start with a clean install rm -rf ./node_modules/ .next/ ./package-lock.json && npm i

when i do npm run dev it hangs. doesn’t respond to curl. i see the listener on port 3000, via lsof -i :3000:

COMMAND   PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    20049 shane   23u  IPv6 0x5622c9dbee67c0eb      0t0  TCP *:hbci (LISTEN)

so the server is fired up, but i don’t see this message ever:

event - compiled client and server successfully in 3.9s (580 modules)

seems to ignore SIGINT (ctrl-c); have to kill using the port and SIGKILL

wonder if it’s the compiler??? also, randomly see some pages rendered wierd (think mobile styles) and have seen something about waiting for play.google.com while the browser was jamming… my paranoia tells me it’s some android arm64 foolishness going on?

@timneutkens happy to help debug this, show you my project etc, but would be great if we (as devs) can get more introspection as to what’s going on under the hood as it’s pretty hard to divinate this black box.

Encountering the same issue on next.js 12, super frustrating to deal with as there is no indication anywhere what the problem is or how to begin debugging it.

creating a production build and using next start works fine but next dev causes requests to spin forever, deleting node_modules, .next and package-lock and reinstalling solves nothing.

checking out known good versions of my code base (that didn’t show this issue prior) and running npm run next dev doesn’t work either. These two things bring me to the conclusion the issue cannot be within my code.

The next.js framework is extremely elegant but this one particular issue makes it very hard to trust it to develop applications on, because at arbitrary points it becomes unresponsive and impossible to progress or fix issues raised by customers, and instead must waste hours trying to fix a problem with no clear route to a solution.

I was having the same problem. I changed npm to yarn and everything started working again.

It appears the port 3000 is already in use. You can solve the problem by doing the following: On Windows:

netstat -ano | findstr :3000
taskkill typeyourPIDhere 

On Linux/Mac:

lsof -i tcp:3000
kill -9 PID

I had a similar problem, for me it was because I accidentally had created an asynchronous client component (switched from a server component and forgot to make it synchronous.) I didn’t get any error messages, just a loading page so it was a bit tricky to debug.

For me, the issue was really weird. When I ran lsof -i tcp:3000 there was a node instance running (probably NextJs) but when I ran npm run dev I didn’t get an error. NextJs started just fine. I somehow got two NextJs instances running at the same time, so when I tried to reach 127.0.0.1:3000, it just wouldn’t load. I killed that process and the issue was fixed.

@next/font was the culprit in my case 🤦‍♀️

that’s it removing @next/font solves the problem

It looks like all of the users with the problem have this in common:

  • Mac OS
  • 100% CPU loop that lasts several minutes
  • Does not respond to Ctrl-C (observed by some as “port 3000 still in use” when trying to restart)

The problem is this:

https://github.com/nodejs/node/issues/43916

The issue is Mac-specific because there is a Mac-specific code case that can infinitely loop in node’s libuv; no other platform has that condition, which is why you won’t see this problem on production Linux hosts, for example.

There is a loop when a libuv write function on Mac returns EPROTOTYPE. There was unsafe code that would infinitely loop in native libuv code (never going back up to V8, not even on Ctrl-C interrupt). It looks like Mac OS may have changed this edge case OR some VPN software aggravates this (as reported in libuv’s tickets) OR something about next.js causes this condition to get hit more often.

libuv already found and fixed this problem, but the fixes are not yet in node:

If you want to see if this is causing your problem, do the following:

  • Install Xcode if you have not already
  • Install Xcode CLI tools if you have not already: xcode-select --install
  • Monitor for node going into a 100% CPU loop with Activity Monitor
  • When node is in a 100% CPU loop
    • Get the PID of node from Activity Monitor
    • Attach a debugger: lldb -p [pid]
      • Note: this does not have a to be a special local build, the standard builds of node are note stripped so you will be able to see at least function names
      • When the debugger attaches the process is paused automatically
      • Run the bt command to get a backtrace for the current thread
      • You should see uv__try_write at the top of the stack if this problem is impacting you
      • To confirm, repeat these a few times and you should see it continue to be in the same place:
        • continue
        • process interrupt
        • bt
        • Repeat a few times - It should always be in the same place
        • kill to detach and kill the node process

Running bt in lldb - Stack of infinite loop

image

temporary fix: delete node_modules and then run npm i then delete .next folder in the project

note that on windows it’s harder to remove these files so I used git bash and then ran ‘rm -rf node_modules’ and ‘rm -rf .next’ then ‘npm i’ then ‘npm run dev’

Simple updated answer, just delete .next folder in the project and then run npm run dev and it should work

I’m running "next": "12.0.3" and I’m having the same problem, changing ports doesn’t work. any idea on how to fix this?

Hi

This same issue occurred for me because of code mistake.

I have created one function component Nav.js file

function Nav() { return ( <Nav> <h2>nav</h2> </Nav> ) } export default Nav

here the mistake is above code instead of <Nav></Nav> we need to use <nav></nav>. I accidentally return this and import into index.js file and the code is going into infinite server gets stuck after a while, requests just spin. After change this correct code function Nav() { return ( <nav> <h2>nav</h2> </nav> ) } export default Nav

After this everything works charm. Check after which code added this issue occurred. Not sure if your problem is due to the same cause.

what about having same issue on production mode too? should i make a way to force refresh the page?

Deleting and rebuilding .next and node_modules made no difference for me. Disabling my home VPN did enable localhost to load. Suggests external dependency being blocked. I have a fresh ‘create-next-app’ install, no other dependencies added. node v18.16 next.js v13.4.1

Deleting .next folder in the project and then running npm run dev simply worked for me.

@talksik, Thank you!

This worked for me! Thanks!

No, I’m still having this problem. next@13.2.1

Anyone having this problem on a Mac: try the node 18.7.0 release just to see if the problem goes away as the infinite loop in native code fixes are in that version.

https://github.com/nodejs/node/releases/tag/v18.7.0

Deleting .next folder in the project and then running npm run dev simply worked for me.

@talksik, Thank you!

I realized why is this happening… (I hope) Your browser stores old cache and tries to load it. Switching your port causes not reading from the cache (because the cache isn’t store on this new port). Also clearing cache solves this issue, but in a moment I get again in the same problem with the infinite loading…

I had the same issue for the last couple of months. Yesterday I finally had it and removed all node_modules, and package-lock, updated my npm to 7.x, stopped the service worker for localhost:3000, and started using npx next dev instead of npm run dev. And it seems the issue was solved.

It seems that what made it work was stopping the service worker and then restarting the npx next dev command.

I don’t know if it has anything to do with it but the first Next app I worked on was the PWA example and I worked on it for several weeks before disabling the PWA for development.

Problem persists lol. I have to restart my app after a little while of development. Using Next v10.

+1 Running latest version (9.5.3) and I am getting this problem too. ~I think it might be related to Sass. When I comment out my global @import for external library styles it works properly.~ After the first change it hangs

webpack-hot-middleware uses up one of the 6 connections via EventSource, and when all are used up it results in the browser being unable to establish more connections and therefore an infinite loading spinner: https://github.com/webpack-contrib/webpack-hot-middleware/issues/298

Maybe there’s a rogue service worker that hogs up more and more connections over time and never releases them. Just guessing though.

Of course it should be solved! I’m just trying to help find the root cause. 🙂

Might help to eliminate if it’s now dev or next dev related by trying to run your app with just Next.js.

The point about Typescript, you are correct! I faced the same issue recently where next.js was logging complied successfully but infinitely loading the webpage. I never imagined this could be because of Typescript. I actually created a custom type and using it in multiple files by exporting it from a file. Now, I am still figuring out why exactly is next.js showing this behaviour with custom types. Did you find any clue?

One possible reason for freezing is that Next.js is trying to download a font but the server is throttling your requests. It might be that you are hitting a limit of the font provider.

I was using Inter font from Google. The issue was gone when I either

  • I removed next/font usage, or
  • I switched my internet connection, changing my IP

Could we define timeout via env var or display a better error text if font fetching takes a long time?

@Stroik for that case make sure you’re using next@canary currently (or 13.3.0 which is going out right now).

can this be closed?

For me this was caused by the usage of react-toast-notifications package usage, removing it resolved this issue for me. If you recently added a package, do checkout by removing it first.

For me it was caused by an image (used next/image) not loading due to a missing file, once I fixed it the problem went away.

It looks like all of the users with the problem have this in common

There are several users experiencing this issue on Linux and Windows in this thread (including myself)

Hi I have some issue when i reload page then i fetch long time page waiting process… 1 min waiting then page reloaded. How to fix this issue ?

I had the same issue. I deleted the node_modules and package-lock.json file and re-installed the node_modules by runnnig npm install and now it refreshes the browser when saving any file.

I don’t really know about OS level resource management but it seems like it’s happening in macOS or Linux as well, in my case I’m on wsl2 and shutting down it in Powershell by wsl --shutdown and boot wsl again made it work.

Just rebooting the machine didn’t work, I needed to kill wsl process in powershell and now it’s working as intended.

In my case changing port didn’t work FWIW.

I assume wsl is still a bit unstable regarding network configuration etc but who knows…

Same issue on nextjs 10.0.6

For me it was the service worker that was completely messing with my browser creating huge memory leaks in the hundreds of MB, preventing the page to reload, preventing routes to work properly and other negative effects. I resolved all those issues when I stopped and unregistered the service worker in the chrome dev tools.

Having trouble consistently reproducing it to investigate, if you run into it please provide steps to reproduce or a repository, I’d love to investigate further but I could only investigate it about 5 minutes while trying for 2 hours.

Posting +1 or “same issue” is not helpful. Please provide a reproduction so that it can be investigated.

Version: 9.5.2

Same here after a while, especially when inspect is open, it gets stuck I tried to run it on the different port next dev -p * but didn’t change

it’s really annoying 😒

We are facing the same issue… Everytime we have a new release, our platform cannot load any new pages on the client machines until they refresh their cache. As we are a B2C company, its hard to notify and teach the users about this…

This sounds unrelated to the issue @mjm reported which only manifests in the development server, which I doubt you’re running in production. Can you create a discussion on https://github.com/vercel/next.js/discussions with clear and concise reproduction steps.

@HunderlineK I also experience that but I think it is another issue… Hope we can finally find root-cause(s) together 😃

@HunderlineK This also applies to next dev. But it’s interesting that Nuxt has the same issue, might be some shared dependency that’s causing this?

@eandre I have and I think the issue came back eventually. Anyway, this is an issue on next.js and should be solved

@pablopunk have you tried cleaning out the cached service worker? That cache will be port-specific, so by changing the port you’re working around the problem. Curious to see if this is the root cause for others or just for me.

Hello I am facing the same problem too! Been learning Next for two hours now and I thought I installed it wrongly. The issue can be mostly reproduced (in my case) by writing incorrect React code so that it compiles with errors. After correcting it, the hot reload function goes abit wonky and I have to kill the dev server (sometimes multiple times) to get it to work again.

My dependencies are as follows: "dependencies": { "next": "^9.3.6", "nprogress": "^0.2.0", "react": "^16.13.1", "react-dom": "^16.13.1" }

Just for comparison, Gatsby’s start script works extremely well on my computer (not sure if this is worth anything)

Should I downgrade from 9.3.6 to a stable version?

(This is my third month self learning coding so if I sound dumb I apologise)

next-portfolio.zip