Webpack and TypeScript: Implementing Moment.js Plugins

I am looking to integrate the JDateFormatParser plugin with Moment.js in my Angular 4 application.


Successfully installed and implemented Moment.js using:
npm install moment --save

Imported it into my .ts file:

import * as moment from 'moment';

Everything is working as expected.


Next, I added the JDateFormatParser plugin by running:

npm install moment-jdateformatparser --save

When trying to use it in my code:

moment().toJDFString(moment.localeData().longDateFormat('L'))

My IDE showed an error message:

TS2339:Property 'toJDFString' does not exist on type 'Moment'.


Referring to this answer, I attempted to fix it using casting:

(<any>moment()).toJDFString(moment.localeData().longDateFormat('L'))

The IDE error was resolved, but now the console displays:

TypeError: WEBPACK_IMPORTED_MODULE_6_moment(...).toJDFString is not a function


Any suggestions on how to properly utilize this?

Answer №1

When working with the moment-jdateformatparser library, it is necessary to import it in order to add its functions to moment and export moment.

import * as moment from "moment-jdateformatparser";

If you need to use multiple plugins, there isn't a straightforward solution at the moment. One approach could be importing each plugin separately and assigning them different names:

import * as momentJdate from "moment-jdateformatparser";
import * as momentTimezone from "moment-timezone";

You can then merge them using deepExtend if desired:

let moment = {};
deepExtend(moment, momentJdate, momentTimezone);

// You can now use moment().toJDFString() and moment.tz.names()

While these methods may not be ideal, they should work effectively. Personally, I haven't encountered a situation where I needed to use more than one moment plugin in a single file, so this workaround should suffice.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The pipe operator in Angular is failing to function as intended

I encountered an error while using the replace operator in Angular. Can someone help me identify the issue? Check out this link for more information ...

NG-Bootstrap Navigation Bar- dynamic hamburger icon animation

I have been trying to implement an animated hamburger animation on the navbar using ng-bootstrap and Angular, but so far I have had no success. While the Navbar does collapse as expected, it lacks the animation and transformation into an 'x'. I w ...

Exploring the connections among Angular, Angular-CLI, Angular-seed, and Ionic

I'm currently tackling a project in Angular 2. I'm a bit puzzled about the relationship between CLI, Seed, and Ionic with Angular. Are they considered sub-frameworks of Angular? ...

Obtain data from a local JSON file using the http.get() method in Angular 2

I am attempting to utilize the http.get() method in Angular 2 to load a local JSON file. I followed a suggestion from Stack Overflow that involves modifying my app.module.ts: In this example, I have imported the HttpModule and the JsonModule from @angular ...

Angular's ngbdatepicker encountering RangeError: Call stack size has surpassed maximum limit

Below is the code snippet used in my angular component template. <input type="text" (click)="dp.toggle()" ngbDatepicker #dp="ngbDatepicker" id="myDatePicker" autocomplete="off" placeholder= ...

Tips for sending a variable to control a particular panel within an accordion in Angular 2

I have a collection of ngbpanels that are dynamically created using ngFor. I need to expand or collapse a specific panel based on certain conditions, but the ID I provide for the panel is stored in a variable. The code does not recognize the panel ID when ...

The Mat table is not updating on its own

I am facing an issue in my Angular application where a component fetches a hardcoded list from a service and subscribes to an observable to update the list dynamically. The problem arises when I delete an element from the list, as it does not automaticall ...

Angular application fails to register modifications during cypress tests

I am encountering a unique challenge while executing Cypress tests on my angular application. The problem that I face is that the DOM does not update in response to changes from observables or component values during the execution of my cypress tests. Le ...

React - Ensure useEffect is triggered only after state update

One of my components (ItemsIndex) is using a custom hook to fetch data from our API. However, the ItemsIndex's useEffect runs first, causing the DOM to not be filled with elements that could be scrolled into view. How can I make sure that the useItems ...

Saving a picture to your Ionic device

I am currently using the code snippet below to access the device's Photo Library. However, it is returning a base64 encoded string, which has left me feeling unsure of how to proceed with this information. My goal is to save the photo to the applicati ...

The element of type 'OverridableComponent<LinkTypeMap<{}, "a">>' cannot be assigned to a 'ReactNode'

I'm currently working on a project where there's a component named ListItemTextStyle. Within that component, the prop type is defined as follows: import { LinkProps, ListItemButtonProps, } from '@mui/material'; type IProps = LinkP ...

Typescript's Array extension causes issues with constructors

During my experimentation with TypeScript, I encountered an interesting behavior: class ExtArray<U> extends Array<U> { constructor(...args : U[]) { super(...args); } public contains(element : U) : boolean { var i ...

How to eliminate subdomains from a string using TypeScript

I am working with a string in TypeScript that follows the format subdomain.domain.com. My goal is to extract just the domain part of the string. For example, subdomain.domain.com should become domain.com. It's important to note that the 'subdoma ...

NGRX refresh does not result in any successful actions

Having an issue with loading users into a mat-selection-list within a form. Everything works fine the first time, but upon page refresh, the selector returns 'undefined'. Initially, both GET_USERS and GET_USERS_SUCCESS are triggered (console log ...

What methods can I use to emphasize a row of mat-radio-buttons?

UPDATE: https://stackblitz.com/edit/angular-uhuwie I am struggling to implement row highlighting for a specific mat-radio-button. Currently, all rows are being highlighted when the correct choice is selected. The desired behavior is that only the row corr ...

Leveraging npm for the development of my TypeScript/Node.js project

I'm facing challenges working on a project written in TypeScript and running on Node. I am finding it difficult to write the npm script to get it up and running properly for development purposes. What I am attempting to achieve is: clear the /dist f ...

tips for populating input form with initial retrieved data

Hey there! I am currently working on fetching some data and my goal is to display these values as the initial input in a form. However, I am encountering an issue where instead of displaying the values, I am getting "undefined" even though I can see the da ...

What could be causing the sudden triggering of ngOnInit in Angular 4?

After sending a request to the server, ngOnInit() { this.service.get(1).subscribe((response) => { this.whenDateCurrent = new DateCustomDate(); }); } Within the same component, there is a method that retrieves this.whenDateCurrent: public getCurre ...

What is the method for incorporating a reverse pipe into *ngFor that leverages an Observable?

I am developing an Ionic app that utilizes Firestore and AngularFire auto IDs for adding items to the collection. I am attempting to retrieve the Firestore collection and display it in reverse order. However, when I try to use the 'reverse' pipe, ...

The click event was not captured by the Angular innerhtml element

<div style="text-align: left;margin-left: 10px;flex-grow: 1;"> <div [innerHtml]="secureHtml(control.data.text)" *ngIf="mode!=Mode.MODE_EDITABLE" (click)="handleClick()"></div> ...