Uncaught ReferenceError: moment is not defined while using moment in laravel with vue js

13,761

Solution 1

I use the following way. In app.js I add

window.moment = require('moment');
window.moment.locale('ru');

Solution 2

You should import moment library before starting to use it.

You can try code below

<script>
   import moment from 'moment'
   var order = new Vue({
   ...
Share:
13,761
Aqeel Ali
Author by

Aqeel Ali

Updated on July 17, 2022

Comments

  • Aqeel Ali
    Aqeel Ali almost 2 years

    im using laravel 5 and i installed moment js through npm install command. im trying to use it in one of my views with vue js. but no matter what i do i get error "moment is not defined" and if i use require, i get "require is not defined" below is the script section of my view. `

    <script>
        var order = new Vue({
            el: '#create_order',
    
            data: {
                moment: moment,
                event_id: '',
                event_type: '',
                ticket_types: [],
                time_slots: [],
                current_timeslots: [],
                start_date: null,
                end_date: null
            },
    
            methods: {
                moment: function (date) {
                    return moment(date);
                },`
    

    im beginner at this so please be as explanatory as possible, thanks.

  • Aqeel Ali
    Aqeel Ali almost 7 years
    thanks for your answer but now im getting "unexpected token import"
  • Aqeel Ali
    Aqeel Ali almost 7 years
    is this app.js in resources->assets->js->app.js ? and do i have to do anything in the view? because im still getting moment is not defined