Vue 1.x Moment.js Wrapper
This package, `vue-momentjs` v1.0.0, serves as a lightweight wrapper for the Moment.js library, designed specifically for applications built with Vue.js version 1.x. It simplifies integrating Moment.js functionalities into Vue components by exposing a `$moment()` instance. Released approximately six years ago and last updated around that time, the package is now considered abandoned. Its core dependency, Moment.js, has been in maintenance mode since September 2020 and is not recommended for new projects due to concerns regarding bundle size, object mutability, and modern JavaScript ecosystem compatibility. Furthermore, Vue 1.x has reached its end-of-life, making `vue-momentjs` incompatible with modern Vue 2.x or Vue 3.x applications. Users requiring date manipulation in current Vue projects should consider actively maintained alternatives like Day.js, Luxon, or `date-fns` with dedicated Vue 2/3 integrations.
Common errors
-
TypeError: this.$moment is not a function
cause The `vue-momentjs` plugin was not correctly installed via `Vue.use(VueMomentJS, moment);` or Vue is not an active instance.fixEnsure `Vue.use(VueMomentJS, moment);` is called before instantiating Vue components and that `moment` is properly imported and passed as an argument. -
Vue.use is not a function (when using Vue 3)
cause The global `Vue.use` method was removed in Vue 3. Plugin installation is now done on the application instance (e.g., `createApp().use(plugin)`). This package is only compatible with Vue 1.x.fixThis package is not compatible with Vue 3. You must use a Vue 3 compatible date library and its corresponding plugin or integrate directly. -
Uncaught ReferenceError: moment is not defined
cause The `moment` library was not imported or made globally available before `vue-momentjs` attempted to use it.fixEnsure you have `import moment from 'moment';` at the top of your file and that `moment` is listed as a dependency in your `package.json` and installed.
Warnings
- breaking This package is designed for Vue 1.x and is incompatible with Vue 2.x or Vue 3.x due to significant API changes in newer Vue versions, including the removal of global Vue instance methods for plugin installation in Vue 3.
- deprecated The underlying `moment` library is in maintenance mode and not recommended for new projects. It will not receive new features and has known issues with bundle size and object mutability.
- gotcha The `vue-momentjs` package itself appears unmaintained, with its last publish date being approximately six years ago. This suggests a lack of support, bug fixes, or security updates.
Install
-
npm install vue-momentjs -
yarn add vue-momentjs -
pnpm add vue-momentjs
Imports
- VueMomentJS
import { VueMomentJS } from 'vue-momentjs';import VueMomentJS from 'vue-momentjs';
- VueMomentJS
const { VueMomentJS } = require('vue-momentjs');const VueMomentJS = require('vue-momentjs'); - $moment
this.$moment().format('LLL');
Quickstart
import Vue from 'vue';
import moment from 'moment';
import VueMomentJS from 'vue-momentjs';
// Install the plugin, passing the Moment.js library instance
Vue.use(VueMomentJS, moment);
// Create a basic Vue component
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue with Moment.js!'
},
mounted() {
// Use this.$moment() to access Moment.js functionalities
console.log('Current time:', this.$moment().format('MMMM Do YYYY, h:mm:ss a'));
this.message = `Current time: ${this.$moment().format('MMMM Do YYYY, h:mm:ss a')}`;
},
template: `
<div>
<h1>{{ message }}</h1>
<p>Formatted date from instance: {{ this.$moment('2023-01-15').format('dddd, MMMM Do YYYY') }}</p>
</div>
`
});
// Ensure you have a div with id='app' in your HTML:
// <div id="app"></div>