vincej's avatar
Level 15

Can not get HMR to work in PHPStorm

I have been debugging my JS scripts with Google dev tools. they are very useful, but it is very slow to have to reload a page view everything I make a change view.

So, I have spent the whole afternoon trying to get HMR to work in PHPStorm. The laravel Mix docs are out of date, ChatGPT has been useless.

This has turned into a small nightmare. Does anyone know of a quality way of debugging JS files without having to always reload the page?

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Here’s a step-by-step solution to get Hot Module Replacement (HMR) working with Laravel Mix (Vite or Webpack) in your Laravel project, especially when using PHPStorm:


1. Check Your Laravel Version

  • Laravel 9+ uses Vite by default.
  • Laravel 8 and below typically use Laravel Mix (Webpack).

2. For Laravel 9+ (Vite)

a. Start the Vite Dev Server:

In your project root, run:

npm run dev

or

vite

b. Update .env

Make sure you have:

VITE_DEV_SERVER=true

(If you use a custom port, update accordingly.)

c. Use the Vite Blade Directive

In your Blade template (usually in resources/views/layouts/app.blade.php):

@vite('resources/js/app.js')

d. Access Your App via the Correct URL


3. For Laravel 8 or Mix (Webpack)

a. Start the Mix Hot Server:

npm run hot

b. Update .env

Set:

MIX_HMR=true

c. Use the Mix Helper in Blade

<script src="{{ mix('js/app.js') }}"></script>
<link rel="stylesheet" href="{{ mix('css/app.css') }}">

d. Access Your App via the Correct URL

  • Use the URL provided by the hot server (e.g., http://localhost:8080).
  • If you use Valet or Homestead, you may need to set APP_URL and MIX_HMR accordingly.

4. Common Pitfalls

  • Browser Cache: Clear your cache or use incognito mode.
  • Firewall/Antivirus: Sometimes these block HMR websockets.
  • HTTPS: If your app uses HTTPS, make sure your dev server does too.
  • Network: If using Docker/VM, ensure ports are exposed.

5. Debugging Tips

  • Open your browser’s dev tools and check the console for HMR connection errors.
  • Make sure npm run dev or npm run hot is running and not showing errors.
  • If you see “waiting for update signal from WDS” or similar, HMR is running.

6. Alternative: Live Reload Extensions

If HMR is too tricky, you can use browser extensions like LiveReload or BrowserSync for auto-reloading.


7. PHPStorm Integration

  • PHPStorm itself doesn’t control HMR, but you can use its terminal to run npm run dev.
  • You can also set up a JavaScript Debug configuration to debug JS directly in PHPStorm.

Summary:

  • Use npm run dev (Vite) or npm run hot (Mix).
  • Make sure your Blade templates use the correct helpers.
  • Access your app via the dev server’s URL.
  • Check for errors in the browser console and terminal.

If you share your Laravel version and setup (Valet, Homestead, Docker, etc.), I can give more tailored advice!

Please or to participate in this conversation.