canadianlover's avatar

Vite server - error_connection_refused

I have a Vue component I am serving through Inertia. When I run npm run dev and check it out in the browser, I get the following in the console

Failed to load resource: net::ERR_CONNECTION_REFUSED :5173/resources/js/app.js:1 Failed to load resource: net::ERR_CONNECTION_REFUSED :5173/resources/js/Pages/showpost.vue:1 Failed to load resource: net::ERR_CONNECTION_REFUSED However, when I run npm run build everything works fine. I am using Homestead to serve the page. What's wrong? vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    server: {
        host: 'blog.test',
        https: false,
        cors: false,
        hmr: {
            host: 'blog.test'
        }
    },
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});

showpost.vue

<template>
    <h1>{{post.title}}</h1>
    <small>(posted by {{post.user.name}} at {{post.created_at}} in {{post.blog.title}}</small>
    <hr />
    {{post.body}}
</template>

<script>
    export default {
       props: ['post']

    }
</script>

PostController.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostsController extends Controller
{
    public function show(Post $post): \Inertia\Response|\Inertia\ResponseFactory
    {

        return inertia('showpost')->with(['post' => $post]);
    }
}
0 likes
5 replies
LaryAI's avatar
Level 58

The issue seems to be with the Vite server configuration. The host in the Vite configuration file is set to 'blog.test', but it is not accessible from the browser. To fix this, the host should be set to the IP address of the Homestead machine.

To get the IP address of the Homestead machine, run the following command in the terminal:

homestead ssh -c "hostname -I | cut -d' ' -f2"

This will output the IP address of the Homestead machine.

Update the Vite configuration file with the IP address:

server: {
    host: '192.168.10.10', // replace with the IP address of the Homestead machine
    https: false,
    cors: false,
    hmr: {
        host: '192.168.10.10', // replace with the IP address of the Homestead machine
    }
},

Save the file and restart the Vite server by running npm run dev again. The Vue component should now be accessible in the browser without any errors.

1 like
canadianlover's avatar

...and it isn't working again. No, I am not using Docker and the port looks correct

Please or to participate in this conversation.