Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While using moment in Angular 7 getting Error:TypeError: moment is not a function

followed below steps to install and use moment:

npm install moment --save

In app.component.ts file:

import * as moment from 'moment';



export class AppComponent{
    currentDate = moment();

Also added below code in angular.json as per this article:

"scripts": ["./node_modules/moment/min/moment.min.js"]

I get:

TypeError: moment is not a function

Please check repro demo

If you know why I am getting this error please assist.

like image 764
Always_a_learner Avatar asked Jun 29 '26 18:06

Always_a_learner


2 Answers

import is fine:

import * as moment from 'moment';

You also need to install the type definition so that the typescript compiler can resolve the types specific to moment.js:

npm install @types/moment
like image 106
pixelbits Avatar answered Jul 01 '26 14:07

pixelbits


import * as temp from 'moment'; const moment = temp["default"];

like image 32
Ritin Avatar answered Jul 01 '26 15:07

Ritin