1

Error while trying to use the moment js in angular 7 library (Same is working in normal angular app)

Steps to replicate:

Create angular library

ng generate library my-lib
npm install moment --save

in a component try using below code

import * as moment from 'moment';    

export interface CalendarDate {
  mDate: moment.Moment;
}

export class CalenderComponent implements OnInit {
  currentDate = moment();
...

When we try ng build my-lib --watch It will fail with below error

Building Angular Package
Building entry point 'student'
Compiling TypeScript sources through ngc
Bundling to FESM2015

BUILD ERROR
Cannot call a namespace ('moment')
Error: Cannot call a namespace ('moment')
    at error (D:\Eschool\homework\student\node_modules\rollup\dist\rollup.js:346
0:30)
    at Module.error (D:\Eschool\homework\student\node_modules\rollup\dist\rollup
.js:13349:9)
    at CallExpression.bind (D:\Eschool\homework\student\node_modules\rollup\dist
\rollup.js:10228:30)
    at AssignmentExpression.NodeBase.bind (D:\Eschool\homework\student\node_modu
les\rollup\dist\rollup.js:9183:23)
    at AssignmentExpression.bind (D:\Eschool\homework\student\node_modules\rollu
p\dist\rollup.js:9919:31)
    at ExpressionStatement$$1.NodeBase.bind (D:\Eschool\homework\student\node_mo
dules\rollup\dist\rollup.js:9183:23)
    at BlockStatement$$1.NodeBase.bind (D:\Eschool\homework\student\node_modules
\rollup\dist\rollup.js:9179:31)
    at FunctionExpression.NodeBase.bind (D:\Eschool\homework\student\node_module
s\rollup\dist\rollup.js:9183:23)
    at MethodDefinition.NodeBase.bind (D:\Eschool\homework\student\node_modules\
rollup\dist\rollup.js:9183:23)
    at ClassBody.NodeBase.bind (D:\Eschool\homework\student\node_modules\rollup\
dist\rollup.js:9179:31)
    at ClassDeclaration$$1.NodeBase.bind (D:\Eschool\homework\student\node_modul
es\rollup\dist\rollup.js:9183:23)
    at ExportNamedDeclaration.bind (D:\Eschool\homework\student\node_modules\rol
lup\dist\rollup.js:10641:30)
    at Program.NodeBase.bind (D:\Eschool\homework\student\node_modules\rollup\di
st\rollup.js:9179:31)
    at Module.bindReferences (D:\Eschool\homework\student\node_modules\rollup\di
st\rollup.js:13303:18)
    at Graph.link (D:\Eschool\homework\student\node_modules\rollup\dist\rollup.j
s:21489:20)
    at D:\Eschool\homework\student\node_modules\rollup\dist\rollup.js:21584:19


Compilation failed. Watching for file changes...
Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
Gurpreet Singh
  • 87
  • 1
  • 11

1 Answers1

0

I had the same issue before. To fix it, use moment-es6 in your library instead of moment. Then it'll work.

pjlamb12
  • 2,300
  • 2
  • 32
  • 64