net::ERR_CONNECTION_REFUSED using Laravel 9, ReactJs with vite js
Asked Answered
N

12

27

I'm trying to build an app using Laravel 9 and ReactJS with vite js. I tried following command to build.

npm run dev

But I'm getting following errors,

GET http://[::1]:5173/resources/css/app.css net::ERR_CONNECTION_REFUSED

GET http://[::1]:5173/@vite/client net::ERR_CONNECTION_REFUSED

GET http://[::1]:5173/resources/js/app.jsx net::ERR_CONNECTION_REFUSED

GET http://[::1]:5173/@react-refresh net::ERR_CONNECTION_REFUSED

Noachian answered 20/9, 2022 at 8:9 Comment(3)
Did you solve this issue?Dosimeter
I got this too, do you have any ideas?Desiccant
I developed the same app with Laravel 10 and got no error.Noachian
M
69

If you entered npm run build on production, your .env file looks good, and you still have such errors as the author – just delete the file public/hot.

Monocotyledon answered 7/12, 2022 at 9:28 Comment(7)
Thanks for your help mate, this solved the issue for me deploying from local to docker container into DO App Platform. It was driving me absolutely nuts!Radium
Legend! You saved my deploy. I tried literally anything to fix this problem. I even thought it was because of my webhost.Dimercaprol
This should be accepted as answer!Coachandfour
It is simple solution, awesome! I would like to give your answer 20 votes)))Bang
Weird, I have no idea why this worked but thank you very much.Imponderabilia
I initially put a like on this answer because it seemed to work, but this is not the solution if you care about hot reload (like I do). Deleting the hot file actually stops the hot reload feature of npm run dev. This error is happening because it's trying to use an IPV6, to force an IPV4 you need to specify the server ip in the vite.config.js file: https://mcmap.net/q/495790/-net-err_connection_refused-using-laravel-9-reactjs-with-vite-jsBrakeman
I spent a whole day on this. You're a lifesaver. Thanks!Obsequies
S
8

For those using Laravel Sail, open the vite.config.js file and configure it like so:

export default defineConfig({

  plugins: [
    react(),
    laravel({
        input: ['resources/css/app.css', 'resources/js/app.js'],
        refresh: true,
    }),
  ], 
  server: {
    hmr: {
        host: 'localhost',
    },
  }
});

If need be, stop and restart the server sail npm run dev

Smock answered 28/2, 2023 at 19:12 Comment(0)
F
4

Its Work for me 1-First Run npm run dev in Terminal 2-Then Run npm run build

Fugate answered 4/1, 2023 at 13:33 Comment(1)
Seriously, Didn't know that, we have to first do this step before building. Also one more thing, we have to set the ASSET_URL to the live domain.Katowice
F
3

add host in your vite.config.js, so it will force it to IPv4

export default defineConfig({
    server: {
        host: '127.0.0.1',  // Add this to force IPv4 only
    },
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});
Fluidics answered 6/12, 2022 at 8:32 Comment(1)
This is the real answer! Deleting the "hot" file is disabling the hot reload and it's something I really don't want to do! Thanks!Brakeman
E
2

If you have run npm run dev then you must find a file in public/hot and Rewrite the proper address / URL in it. For me http://127.0.0.1:5173 worked.

Effectual answered 19/6, 2023 at 13:15 Comment(1)
rewrite an autogenerated file, what a solutionVillage
W
1

I think I may have found a solution with the build option called Rollup. When building in production, rollup will remove unused code. In this process it will bundle the required assets and reference them in accordance to the URL that you would be using at that current moment.

To fixed it, you could try this:

export default defineConfig({
      build: {
        rollupOptions: {}
      }
    })

I was helped by a similar issue posted on Github so maybe you could use that as a point of reference. Here is the Discussion

Wahlstrom answered 28/11, 2022 at 14:26 Comment(0)
O
1

This means that your assets are not built yet, use npm run build.

Operable answered 29/11, 2022 at 0:31 Comment(0)
L
0

In my case the issue was that the port 5173 was already in use. I've just freed it - and everything worked again. Hope that helps.

Lithology answered 21/12, 2022 at 15:17 Comment(0)
H
0

You could look at Chrome extensions you have enabled. In my case, it was "uBlock Origin" that was messing with vite dev server

Hike answered 28/3 at 9:55 Comment(0)
B
0

If you care about not losing the Hot Reload feature, deleting the "public/hot" file is something that you shouldn't do!

Hardcoding an IP address is not a possible approach either if you're working on a shared repository and users have different local IPs (this was my case).

In addition to Kidd Tang answer, if you don't want to hardcode a static IP but read it from the APP_URL in the .env file, just edit the vite.config.js like this:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import dotenv from 'dotenv';

dotenv.config();

export default defineConfig({
    server: {
        host: process.env.APP_URL,
    },
    plugins: [
        laravel({
            input: [
                'resources/css/app.scss',
                'resources/js/app.js',
            ],
            refresh: true,
        })
    ],
});

You need to have the dotenv module installed (npm install dotenv)

Brakeman answered 18/4 at 0:27 Comment(0)
F
0

first of all, you need to delete a file the file is public/hot, or you can see below screenshot and the run cmd. ...> npm run build [1]: https://i.sstatic.net/BHnTCw4z.png

Fabiola answered 30/6 at 5:41 Comment(0)
P
-1

This code work for me

export default defineConfig({
    server: {
        host: '10.10.x.30',  // Add this to force IPv4 only
    },
Pleasure answered 29/12, 2023 at 13:24 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Kristin

© 2022 - 2024 — McMap. All rights reserved.