[vite] hmr update /src/App.tsx
// But changes don't appear in the browser
Viteβs hot module replacement should update instantly, but sometimes it stops working.
Fix 1: Full Page Reload Instead of HMR
// β Exporting non-component values breaks React Fast Refresh
export const API_URL = 'https://...'; // π₯ Mixed exports
export default function App() { return <div /> }
// β
Keep components in separate files from constants
// App.tsx β only the component
// config.ts β constants
Fix 2: File Watcher Limit (Linux)
# β Too many files to watch
# β
Increase the limit
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Fix 3: Docker / WSL Network Issues
// vite.config.ts β force polling in Docker/WSL
export default {
server: {
watch: {
usePolling: true,
},
},
};
Fix 4: Wrong File Extension
# β Vite doesn't know how to handle the file
# β
Make sure you're using supported extensions
# .tsx, .jsx, .ts, .js, .css, .scss, .json
Fix 5: Clear Cache
# Delete Vite's cache
rm -rf node_modules/.vite
npm run dev
Fix 6: Check Browser Console
# Look for WebSocket errors in browser DevTools
# If you see "WebSocket connection failed":
# vite.config.ts β fix HMR connection
export default {
server: {
hmr: {
host: 'localhost',
port: 5173,
},
},
};