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

felloz's avatar

Import moment in app.js with Inertia and Vue 3

Hello,

I would like to import the library moment in my app.js file, this is my code but is not working...

import moment from 'moment'

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) =>
        resolvePageComponent(
            `./Pages/${name}.vue`,
            import.meta.glob('./Pages/**/*.vue')
        ),
    setup({ el, app, props, plugin }) {
        return createApp({ render: () => h(app, props) })
            .use(moment) // Here im using it
            .component('InertiaLink', Link)
            .component('Datepicker', Datepicker)
            .use(ZiggyVue, Ziggy)
            .mount(el);
    },
});

Im getting this error:

Uncaught (in promise) TypeError: _ctx.moment is not a function
0 likes
5 replies
christian-qode's avatar

@felloz Why do you want to import it in your app.js file? I suggest you to import it in the specific components that need the package.

felloz's avatar

@Sinnbeck Thanks, but i need to import it globally, I just want to know how to doit. If is a good practice or not, of course not.

thinkverse's avatar

Moment isn't a Vue plugin, but you can easily create a quick wrapper.

import moment from 'moment';

const momentPlugin = {
    install(app) {
        // Attach moment to Vue 3 global properties, to make it globally available.
        // https://vuejs.org/guide/reusability/plugins.html#writing-a-plugin
        app.config.globalProperties.$moment = moment
    }
}

Then you can use that instead of moment.

use(momentPlugin)

Then in your Vue templates, you can use $moment as you would use moment before.

{{ $moment() }}

Please or to participate in this conversation.