How to solve require is not defined?

13,594

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

Share:
13,594
Youssef Boudaya
Author by

Youssef Boudaya

Updated on June 18, 2022

Comments

  • Youssef Boudaya
    Youssef Boudaya almost 2 years

    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'));
    
  • Youssef Boudaya
    Youssef Boudaya over 5 years
    i just updated my question to show the code of my app.js where the error is how should i change that file when now working with requirejs ?
  • Youssef Boudaya
    Youssef Boudaya over 5 years
    so the scripts in my view should be like this:<script type="text/javascript" data-main="js/app.js" src="requirejs.org/docs/release/2.3.5/minified/…> <script src="js/app.js"></script> ?
  • noj
    noj over 5 years
    You can remove the requirejs one. See here for a more detailed example: laravel-mix.com/docs/3.0/workflow