<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Paritosh Singh</title>
    <description>The latest articles on Forem by Paritosh Singh (@proparitoshsingh).</description>
    <link>https://forem.com/proparitoshsingh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F937080%2F1ff0631c-da7d-4f1b-8c1d-ae3704832090.png</url>
      <title>Forem: Paritosh Singh</title>
      <link>https://forem.com/proparitoshsingh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/proparitoshsingh"/>
    <language>en</language>
    <item>
      <title>HMR not working in Vite on WSL2</title>
      <dc:creator>Paritosh Singh</dc:creator>
      <pubDate>Thu, 30 May 2024 11:27:13 +0000</pubDate>
      <link>https://forem.com/proparitoshsingh/hmr-not-working-in-vite-on-wsl2-5h2k</link>
      <guid>https://forem.com/proparitoshsingh/hmr-not-working-in-vite-on-wsl2-5h2k</guid>
      <description>&lt;h2&gt;
  
  
  Vite does not detect file changes
&lt;/h2&gt;

&lt;p&gt;If you are running Vite with WSL2&lt;/p&gt;

&lt;p&gt;If you're using Vite with WSL2 (Windows Subsystem for Linux 2) and encountering issues with Hot Module Replacement (HMR) not working, you're not alone. This is a common problem due to the way WSL2 handles file system notifications. In this post, we'll discuss the issue and provide a solution to get HMR working smoothly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Issue
&lt;/h2&gt;

&lt;p&gt;Hot Module Replacement (HMR) is a feature that allows you to see changes in your application without a full reload. It's a crucial part of the modern development workflow, providing immediate feedback as you make changes to your code.&lt;/p&gt;

&lt;p&gt;When running Vite on WSL2, you might notice that file changes are not being detected, and HMR is not functioning as expected. This happens because WSL2, despite its many advantages, has limitations with file system event propagation. The file system events generated by the Linux subsystem do not always propagate correctly to the Windows host, causing tools like Vite to miss file changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;To work around this issue, we can configure Vite to use polling instead of relying on file system events. Polling involves the development server regularly checking for changes in the file system at specified intervals. While this method can be less efficient and might use more CPU, it ensures that changes are detected reliably in environments like WSL2.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step-by-Step Guide to Enable Polling
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Locate Your &lt;code&gt;vite.config.js&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
Find your &lt;code&gt;vite.config.js&lt;/code&gt; file in the root of your project directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modify the Configuration&lt;/strong&gt;:&lt;br&gt;
Add the &lt;code&gt;server.watch&lt;/code&gt; configuration with &lt;code&gt;usePolling: true&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is an example of what your &lt;code&gt;vite.config.js&lt;/code&gt; might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   // vite.config.js
   import { defineConfig } from 'vite';

   export default defineConfig({
     server: {
       watch: {
         usePolling: true, // Enable polling for file changes
       },
     },
   });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Save and Restart&lt;/strong&gt;:
Save the &lt;code&gt;vite.config.js&lt;/code&gt; file and restart your Vite development 
server to apply the changes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why Use Polling?
&lt;/h2&gt;

&lt;p&gt;Polling forces Vite to check for file changes at regular intervals rather than waiting for the operating system's notifications. This method is particularly useful in environments where these notifications might not be reliable, such as WSL2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reliability&lt;/strong&gt;: Ensures changes are detected even if file system events are missed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-platform Development&lt;/strong&gt;: Ideal for virtualized or containerized setups where traditional file system events may not work correctly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Potential Downsides
&lt;/h2&gt;

&lt;p&gt;While polling solves the HMR issue in WSL2, it comes with some trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased CPU Usage&lt;/strong&gt;: Polling can be more resource-intensive as it involves continuous checks for file changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Impact&lt;/strong&gt;: Might result in slightly slower performance compared to event-driven file change detection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By enabling polling in Vite, you can ensure that HMR works reliably even in WSL2. This small configuration change can significantly improve your development experience, allowing you to take full advantage of Vite's fast and efficient build process.&lt;/p&gt;

&lt;p&gt;If you encounter any further issues or have additional tips, feel free to share them in the comments below!&lt;/p&gt;

&lt;h2&gt;
  
  
  Connect with Me
&lt;/h2&gt;

&lt;p&gt;Feel free to connect with me on social media:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LinkedIn&lt;/strong&gt;: &lt;a href="https://linkedin.com/in/proparitoshsingh"&gt;Paritosh Singh&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Twitter&lt;/strong&gt;: &lt;a href="https://twitter.com/proparitosh1609"&gt;@proparitosh1609&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a href="https://github.com/proparitoshsingh"&gt;proparitoshsingh&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>vite</category>
      <category>wsl2</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
