Reputation: 590
I have installed moment.js
using
npm i moment
Included it in scripts and also tried importing in module.ts
When I am trying to call moment in component.ts
it gives an error as
'cannot find name as moment'.
What am I doing wrong?
Upvotes: 0
Views: 2331
Reputation: 11
Firstly, make sure you have installed and saved moment.js
.
npm i moment --save
Within your component.ts
, import moment as followed:
import * as moment from 'moment';
//You should capable to use momentjs as followed.
console.log(moment(moment.now())).format('YYYY-MM-DD')
Note: If you have trouble importing moment, try adding "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file and then use the syntax
import moment from 'moment';
Upvotes: 1
Reputation: 59
Have you tried using the following:
npm install moment --save
npm install @types/moment --save
Then in the angular-cli.json add to the scripts tag:
"scripts:" ["../node_modules/moment/min/moment/min"]
Then include in component:
import * as moment from 'moment'
Then use in the component method:
let now = moment();
console.log(now.format());
Upvotes: 1
Reputation: 4145
import in your component
import moment from 'moment';
and you can call like this
moment(new Date)
Upvotes: 1