longestdrive's avatar

Laravel Vite issues with CORS policy

Hi Picking up Laravel Vite for the first time and working through issues getting it working on my development set up. I have a fresh install of Laravel 10 and developing using Laravel Homestead.

I first had problems with a Connection Refused which I overcame with updating the config file as follows based on previous questions with the forum:

import { defineConfig } from 'vite';
import laravel, { refreshPaths } from 'laravel-vite-plugin';

export default defineConfig({
    server: {
        host: '192.168.56.56', // replace with the IP address of the Homestead machine
        https: false,
        cors: false,
        hmr: {
            host: '192.168.56.56', // replace with the IP address of the Homestead machine
        }
    },
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: [
                ...refreshPaths,
                'app/Http/Livewire/**',
            ],
        }),
    ],
});

This appears to now work to a degree and I can get the assets built when I run npm run dev

However the browser is reporting multiple errors due to CORS issues:

Acess to script at 'http://192.168.56.56:5173/resources/js/app.js' from origin 'http://golf.local' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

How can I configure this correctly to enable my site to access and overcome the CORS issue?

ANy help appreciated

Thank you

0 likes
2 replies
pindab0ter's avatar

A colleague of mine has the same problem. Have you found a solution yet?

ArthurYdalgo's avatar

I had this problem on two different projects and I discovered that when using

  • "vite": "^4.0.0"
  • "laravel-vite-plugin": "^0.7.8"
  • "@ vitejs/plugin-react": "^3.1" (I put a space there so it wouldn't mention the laracast user "vitejs")

in my package.json, sometimes vite would be bumped up to a version higher than 4.5.5 when installing another package, which would start causing the issue. any other 4.x version after that one would trigger the issue... I just noticed that 4.5.6 was release jan 20th 2025 (~20 days ago at the time of writing).

two possible fixes (both worked for me, but I went for the second one):

1 - keep "vite" at "4.5.5" in your package.json

2 - update "vite" to "^6.0" in your package.json

additionally, you might need to change at least those two packages I mentioned above. I updated them to:

  • "laravel-vite-plugin": "^1.2",
  • "@ vitejs/plugin-react": "^4.3" (I put a space there so it wouldn't mention the laracast user "vitejs")

edit: additional notes: you might to delete your package-lock.json and/or your node_modules directory before attempting another "npm install" to get the new versions

Please or to participate in this conversation.