twg_'s avatar
Level 6

Trying to build my js to work on all browsers

Working on a small app that will have to be ran on any browser. When I run mix.babel on my js file and then try to load the page, it complains that "Uncaught ReferenceError: require is not defined". This is due to the ./bootstrap js file that has multiple require calls. Any suggestions?

0 likes
6 replies
ejdelmonico's avatar

Show your webpack.mix.js file. Are you getting this error during webpack processing or when the file is loaded in the browser?

twg_'s avatar
Level 6

The error I referenced is when I try to load the file in the browser. I have no errors when I try to compile my JS.

let mix = require('laravel-mix');

mix.babel('resources/assets/js/app.js', 'public/js/app.js');

if(mix.inProduction())
{
    mix.version();
}
ejdelmonico's avatar

Hmm, I would get rid of babel for now and use js...that will accomplish the same thing. You should not have a require method in your js after processing. The browser does not understand require() because it is a node function. So, that tells me that the file is not being processed correctly even though its throwing no errors or warnings (sometimes you need to scroll up to see warnings). I would try that way first and if still the issue, start looking at how you are using require in your code.

twg_'s avatar
Level 6

I was trying to write in ES2015 but compile it so that older browsers could still render it. Writing an app where people might be using IE7 and have to support it. :(

ejdelmonico's avatar

Yikes, well the mix.js method will do the same thing because it uses babel anyhow. I would see if it works for you. If you need to adjust the config for older browsers then you can modify the webpack.mix.js file with the new code. The docs explain how to do it so that your config code is merged in.

Please or to participate in this conversation.