Youssef Boudaya
Youssef Boudaya

Reputation: 133

How to solve require is not defined?

I followed this tutorial to create a chat app with and .

I get require is not defined error in app.js:8.I tried so many things but but nothing seems to solve this problem.

I tried using requirejs so i added these two line in my view

<script type="text/javascript" data-main="{{ url('resources/assets/js/app.js')}}" src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
<script src="{{ url('resources/assets/js/app.js')}}"></script>

and this is my app.js code:

requirejs.config({
baseUrl: 'js/lib',
paths: {
    app: '../app'
}
});

requirejs(['jquery', 'canvas', 'app/sub'],
 function   ($,        canvas,   sub) {
});
// this line below is causing the problem
require('./bootstrap');

window.Vue = require('vue');
Vue.use(require('vue-chat-scroll'));

Vue.component('chat-message', require('./components/ChatMessage.vue'));
Vue.component('chat-log', require('./components/ChatLog.vue'));
Vue.component('user-log', require('./components/UserLog.vue'));
Vue.component('chat-composer', require('./components/ChatComposer.vue'));

Upvotes: 7

Views: 23875

Answers (2)

noj
noj

Reputation: 6759

You shouldn't be referencing resources/assets/js/app.js directly. require is used by webpack which is a tool that will bundle your js assets into a single file.

Instead you should run npm run development and then reference js/app.js like here

See laravel mix docs

Upvotes: 5

hbled
hbled

Reputation: 11

You may have to add this to your project : https://requirejs.org/docs/release/2.3.5/minified/require.js

There is a similar issue for your problem right there : Javascript require() function giving ReferenceError: require is not defined

Upvotes: 1

Related Questions