vite: Vite HMR not working . Changing code does not reflect on page until restart vite server

Describe the bug

I setup a basic react+ts template and expect HMR to work. But It does not. When I change some values inside App.tsx / main.tsx or any components, it does not updates in browser. I’m on macOS m1 using vite 5.2.6 and node 18.

I did setup chokidar simple test to check if events get trigged. It works!

Reproduction

pnpm list
Legend: production dependency, optional only, dev only

dependencies:
@reduxjs/toolkit 2.2.2     globals 14.0.0             react-redux 9.1.0          
antd 5.15.4                lodash 4.17.21             styled-components 6.1.8    
chokidar 3.6.0             react 18.2.0               twin.macro 3.4.1           
flexlayout-react 0.7.15    react-dom 18.2.0           vite 5.2.6                 
fsevents 2.3.3             react-icons 5.0.1          vite-tsconfig-paths 4.3.2  

devDependencies:
@openapitools/openapi-generator-cli 2.13.1  eslint 8.57.0                               
@types/node 20.11.30                        eslint-plugin-react-hooks 4.6.0             
@types/react 18.2.72                        eslint-plugin-react-refresh 0.4.6           
@types/react-dom 18.2.22                    openapi-typescript-codegen 0.25.0           
@typescript-eslint/eslint-plugin 7.4.0      sass 1.72.0                                 
@typescript-eslint/parser 7.4.0             tailwindcss 3.4.1                           
@vitejs/plugin-react 4.2.1                  typescript 5.4.3                

cat index.cjs 
const chokidar = require('chokidar');

// One-liner for current directory
chokidar.watch('**/*.tsx').on('all', (event, path) => {
  console.log(event, path);
});
-------
----
--
add node_modules/.pnpm/@ampproject+remapping@2.3.0/node_modules/@jridgewell/trace-mapping/dist/types/trace-mapping.d.ts
add node_modules/.pnpm/@ampproject+remapping@2.3.0/node_modules/@jridgewell/trace-mapping/dist/types/types.d.ts
**change src/components/News/index.tsx**

http://google.com

Steps to reproduce

 npm create vite@latest
✔ Project name: … test-vite
✔ Select a framework: › React
✔ Select a variant: › TypeScript

Scaffolding project in 

Done. Now run:

  cd test-vite
  npm install
  npm run dev

 npm install

added 218 packages, and audited 219 packages in 7s

41 packages are looking for funding
  run `npm fund` for details

System Info

npx envinfo --system --npmPackages '{vite,@vitejs/*,rollup}' --binaries --browsers
Need to install the following packages:
envinfo@7.11.1
Ok to proceed? (y) y

  System:
    OS: macOS 13.6.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 169.16 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/Library/Caches/fnm_multishells/80450_1711424646646/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 10.2.4 - ~/Library/Caches/fnm_multishells/80450_1711424646646/bin/npm
    pnpm: 8.15.5 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 123.0.6312.59
    Safari: 17.4
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.2.1
    vite: ^5.2.0 => 5.2.6

Used Package Manager

npm

Logs

No response

Validations

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 24 (1 by maintainers)

Most upvoted comments

I was able to resolve my issues by adding server in my vite.config.ts:

export default defineConfig({
  plugins: [react()],
  server: {
    watch: {
      usePolling: true
    }
  }
})

Why exactly? No idea, found it on the internet 🤷

Edit: well, documentation explains it pretty clearly:

When running Vite on WSL2, file system watching does not work when a file is edited by Windows applications (non-WSL2 process). This is due to a WSL2 limitation. This also applies to running on Docker with a WSL2 backend.

I also have issues with HMR, it appears that HMR wont work if Vite is not installed at the top level of your diretory, I made a sample app in vite-svlte/vite-project, and on initial install HMR worked, closed VS Code and cd into vite-project and HMR no longer works. On Windows 11.

Please disable auto close, engagement alone should keep this thread alive.

On Thu, 13 Feb 2025, 12:21 pm github-actions[bot], @.***> wrote:

Hello @neodino https://github.com/neodino. Please provide a minimal reproduction https://stackoverflow.com/help/minimal-reproducible-example using a GitHub repository or StackBlitz https://vite.new. Issues marked with needs reproduction will be closed if they have no activity within 3 days.

— Reply to this email directly, view it on GitHub https://github.com/vitejs/vite/issues/16284#issuecomment-2655217359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGVVHCIS4CHMKHZPFDHRFJL2PPXRDAVCNFSM6AAAAABFJ32KROVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNJVGIYTOMZVHE . You are receiving this because you were mentioned.Message ID: @.***>

I am also getting the same issue HMR is not working, even the changes are not being populated after the manual restart and i have tried incogito , removed the cache, and re install the packages the problem still persists. vite version i am using : VITE v5.4.9 system : macOS m1 chip

Same problem, when i change .ts, it dosn’t update in browser. I’m on macOS i7 using vite 5.2.0 and node 18. and After i change vite version to 4.3.1, hmr work.

Seems like not all HMR affected. In our case - changes to the class/styles are not picked up

I have the same issue with a monorepo achitectutre. I tried both Chrome and Firefox even Safari

I even forced HMR by using server: { hmr: true } on vite config file