Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FabioPacifici's avatar

Laravel vite plugin - laravel function not defined.

Hi folks! 👋 Anyone having problems with the vite plugin for laravel from Laravel v10.10 and up?

When npm run dev runs it returns an error due to a require('path') used in the vite.config.js file. If require('path') is replaced with import path from 'path' the dev server returns another error about the laravel function being undefined. To fix the issue in both cases I found that:

  • removing from the laravel package.json file the "type": "module" fix the issue
  • rename the vite.config.js file to vite.config.cjs fix the issue.

Below my vite.config.js file

import {
    defineConfig
} from 'vite';
import laravel from 'laravel-vite-plugin';
const path = require('path') // <-- require path from node
//import path from 'path'; // commenting the about out and enabling this line will make the laravel function undefined.

export default defineConfig({
    plugins: [
        laravel({
            // edit the first value of the array input to point to our new sass files and folder.
            input: ['resources/scss/app.scss', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
    // Add resolve object and aliases
    resolve: {
        alias: {
            '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
            '~resources': '/resources/'
        }
    }
});

In previous laravel versions the type:module wasn't there so the code had no issues. I am wondering what could break from a Laravel prospective if the vite.config file is renamed to .cjs or if type:module is removed from the package.json.

Cheers!

0 likes
0 replies

Please or to participate in this conversation.