DEV Community

Cover image for HMR not working in Vite on WSL2
Paritosh Singh
Paritosh Singh

Posted on

HMR not working in Vite on WSL2

Vite does not detect file changes

If you are running Vite with WSL2

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.

The Issue

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.

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.

The Solution

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.

Step-by-Step Guide to Enable Polling

  1. Locate Your vite.config.js:
    Find your vite.config.js file in the root of your project directory.

  2. Modify the Configuration:
    Add the server.watch configuration with usePolling: true.

Here is an example of what your vite.config.js might look like:

   // vite.config.js
   import { defineConfig } from 'vite';

   export default defineConfig({
     server: {
       watch: {
         usePolling: true, // Enable polling for file changes
       },
     },
   });
Enter fullscreen mode Exit fullscreen mode
  1. Save and Restart: Save the vite.config.js file and restart your Vite development server to apply the changes.

Why Use Polling?

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.

  • Reliability: Ensures changes are detected even if file system events are missed.
  • Cross-platform Development: Ideal for virtualized or containerized setups where traditional file system events may not work correctly.

Potential Downsides

While polling solves the HMR issue in WSL2, it comes with some trade-offs:

  • Increased CPU Usage: Polling can be more resource-intensive as it involves continuous checks for file changes.
  • Performance Impact: Might result in slightly slower performance compared to event-driven file change detection.

Conclusion

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.

If you encounter any further issues or have additional tips, feel free to share them in the comments below!

Connect with Me

Feel free to connect with me on social media:

Happy coding!

Top comments (0)