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

wizard2000's avatar

issue in compiled file with vite in laravel

hi, i 've faced to something in laravel when i use vite. when a run this commend

npm run dev

everything is fine and all my file ,js, images and css , work fine apparently. the problem is when i use "npm run build" to build production version of my assets file . i've got an error :

Uncaught ReferenceError MakeChangeOnData is not defined at HTMLSelectElement.onchange

i've defined the function 'MakeChangeOnData' in a js file in /resources/js/mycustom.js beside of some other codes. but when in compiled file in build directory of Vite this function is not compiled so i will get above error.

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like the function "MakeChangeOnData" is not being included in the compiled file. One solution could be to explicitly import the file containing the function in the main entry file for Vite.

Assuming the file containing the function is named "mycustom.js" and is located in the "/resources/js" directory, you can add the following import statement in your main entry file (e.g. "app.js"):

import './mycustom.js';

This will ensure that the "mycustom.js" file is included in the compiled file and the "MakeChangeOnData" function is defined.

Alternatively, you can try adding the "mycustom.js" file to the "scripts" section of your package.json file, like so:

"scripts": {
    "dev": "vite",
    "build": "vite build",
    "mycustom": "vite build --mode=production --entry=resources/js/mycustom.js"
}

Then, run the following command to build the "mycustom.js" file separately:

npm run mycustom

This should generate a compiled file for "mycustom.js" that includes the "MakeChangeOnData" function.

wizard2000's avatar

@LaryAI I've got this error in five other files. So i think it is not wise to use this way. Also, I've used nWidart modules for my projects if i use this code for each of my modules and it would take dynamic from my site and CMS.

and about first solution, again it is not compiled again even in app.js

wizard2000's avatar

i've found the problem but not solution. Vite remove unused function or variable in JS files so after compiling because i hadn't use it directly in the file so vite will remove it in compiled file. is there any solution to prevent this?

1 like

Please or to participate in this conversation.