<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged hmr</title>
<link>https://coderlegion.com/tag/hmr</link>
<description>Powered by Question2Answer</description>
<item>
<title>How to Fix React (Vite) &amp; Next.js Auto-Refresh Not Working on Windows</title>
<link>https://coderlegion.com/21810/how-to-fix-react-vite-next-js-auto-refresh-not-working-on-windows</link>
<description>&lt;hr&gt;
&lt;p&gt;title: How to Fix React (Vite) &amp;amp; Next.js Auto-Refresh Not Working on Windows&lt;br&gt;
published: true&lt;br&gt;
description: Have you ever saved a file in your React or Next.js project and... nothing happened? Here is how to fix HMR on Windows using polling.&lt;br&gt;
tags: nextjs, react, hmr, webdev&lt;br&gt;
cover_image: &lt;a rel=&quot;nofollow&quot; href=&quot;https://cdn.hashnode.com/uploads/covers/69513d1ce0cbcddf469383e9/3769c10a-2165-4399-acd4-a472f6ea0817.png&quot;&gt;https://cdn.hashnode.com/uploads/covers/69513d1ce0cbcddf469383e9/3769c10a-2165-4399-acd4-a472f6ea0817.png&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;canonical-url-https-blog-abdulrdeveloper-me-how-to-fix-react-vite-next-js-auto-refresh-not-working-on-windows&quot;&gt;canonical_url: &lt;a rel=&quot;nofollow&quot; href=&quot;https://blog.abdulrdeveloper.me/how-to-fix-react-vite-next-js-auto-refresh-not-working-on-windows&quot;&gt;https://blog.abdulrdeveloper.me/how-to-fix-react-vite-next-js-auto-refresh-not-working-on-windows&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Have you ever saved a file in your React or Next.js project and... nothing happened? You go to the browser, manually refresh it, and only &lt;em&gt;then&lt;/em&gt; the change shows up. Or maybe you have to kill the dev server and start it again just to see your new code.&lt;/p&gt;
&lt;p&gt;Most beginners think this is a bug in their code. But let me tell you, it is actually one of the most common issues you will face if you are coding on Windows.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;why-does-this-happen-behind-the-scene-&quot;&gt;Why Does This Happen Behind the Scene??&lt;/h3&gt;
&lt;p&gt;When we run our dev server, Vite and Next.js use a &quot;file watcher&quot; to detect changes and trigger Hot Module Replacement (HMR). On macOS or Linux, this file watcher works perfectly and instantly. But on Windows, these file system events often fail to fire.&lt;/p&gt;
&lt;p&gt;Because the watcher misses the change, HMR never triggers, and you are stuck manually refreshing.&lt;/p&gt;
&lt;p&gt;The fix? We just need to tell our server to actively check for changes every single second instead of waiting to be notified. In programming, this is called &lt;strong&gt;polling&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;fix-for-react-vite-&quot;&gt;Fix for React (Vite)&lt;/h3&gt;
&lt;p&gt;If you are building with Vite, the fix is super simple. Open your &lt;code&gt;vite.config.ts&lt;/code&gt; and just add this &lt;code&gt;server&lt;/code&gt; option:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TypeScript&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;export default defineConfig({
  plugins: [react()],
  server: {
    watch: {
      usePolling: true,
    },
    hmr: {
      host: 'localhost',
    },
  },
})
&lt;/code&gt;&lt;/pre&gt;
&lt;hr&gt;
&lt;h3 id=&quot;fix-for-next-js&quot;&gt;Fix for Next.js&lt;/h3&gt;
&lt;p&gt;Next.js is a bit tricky here. Next.js 16 uses Turbopack by default, but our polling fix only works with &lt;strong&gt;Webpack&lt;/strong&gt;. So, first, we need to disable Turbopack.&lt;/p&gt;
&lt;p&gt;Go to your &lt;code&gt;package.json&lt;/code&gt; and update your dev script to this:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;JSON&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;&quot;scripts&quot;: {
  &quot;dev&quot;: &quot;next dev --webpack&quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, open your &lt;code&gt;next.config.ts&lt;/code&gt; and add this configuration:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TypeScript (or if you are on JS)&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;import type { NextConfig } from &quot;next&quot;;

const nextConfig: NextConfig = {
  webpack: (config) =&amp;gt; {
    config.watchOptions = {
      poll: 1000,
      aggregateTimeout: 300,
    };
    return config;
  },
};

export default nextConfig;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;What is happening here?&lt;/strong&gt; &lt;code&gt;poll: 1000&lt;/code&gt; means Webpack will check for file changes every 1000 milliseconds (1 second). &lt;code&gt;aggregateTimeout: 300&lt;/code&gt; adds a tiny delay so that if you mash &lt;code&gt;Ctrl+S&lt;/code&gt; rapidly, it doesn't trigger multiple rebuilds at the same time. Restart your dev server, and auto-refresh will finally work!&lt;/p&gt;
&lt;hr&gt;
&lt;h4 id=&quot;-important-disable-this-configuration-before-deploying&quot;&gt;⚠️ Important: Disable this configuration before deploying&lt;/h4&gt;
&lt;p&gt;This Webpack polling fix is strictly for local development on Windows. Production platforms like Vercel run on Linux—where file-watching works natively—and build using Next.js 16's default Turbopack engine. Leaving this custom Webpack configuration active in your repository will cause your production build to fail.&lt;/p&gt;
&lt;p&gt;How to handle it:&lt;/p&gt;
&lt;p&gt;Locally: Keep this config active to ensure a smooth development workflow.&lt;/p&gt;
&lt;p&gt;Production: Comment out or remove the custom Webpack config and the --webpack flag before pushing your changes to GitHub or Vercel.&lt;/p&gt;
&lt;p&gt;You can easily re-enable it whenever you need to work locally again.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;what-if-you-are-using-wsl2-&quot;&gt;What if you are using WSL2?&lt;/h3&gt;
&lt;p&gt;If you are running WSL2 and still facing this, the problem is likely where your project is stored.&lt;/p&gt;
&lt;p&gt;Your project needs to live &lt;strong&gt;inside the WSL filesystem&lt;/strong&gt; — at a path like &lt;code&gt;/home/yourname/projects/&lt;/code&gt;. If your project is sitting on your Windows drive (like &lt;code&gt;/mnt/c/Users/...&lt;/code&gt;), file events will not work properly and polling alone might not save you. Move the project inside &lt;code&gt;/home/&lt;/code&gt; and the issue goes away.&lt;/p&gt;
&lt;p&gt;This Next.js issue tripped me up for a while — just adding &lt;code&gt;watchOptions&lt;/code&gt; does not work until you switch back to Webpack first. Once I figured that out, everything clicked.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;You can find more of my work at &lt;a rel=&quot;nofollow&quot; href=&quot;http://abdulrdeveloper.me&quot;&gt;&lt;strong&gt;abdulrdeveloper.me&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Read more posts at &lt;a rel=&quot;nofollow&quot; href=&quot;http://blog.abdulrdeveloper.me&quot;&gt;&lt;strong&gt;blog.abdulrdeveloper.me&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/21810/how-to-fix-react-vite-next-js-auto-refresh-not-working-on-windows</guid>
<pubDate>Fri, 03 Jul 2026 04:53:39 +0000</pubDate>
</item>
</channel>
</rss>