generator-angular2-library: Cannot call a namespace ('moment')
My library uses moment import * as moment from 'moment'.
When I run gulp compile I hit this error.
Error: Cannot call a namespace ('moment') at error (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:185:14) at Module.error (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:8170:3) at CallExpression.bind (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:6324:17) at eachChild.child (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:5674:34) at keys.forEach.key (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:5685:5) at Array.forEach (native) at ConditionalExpression.eachChild (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:5678:13) at ConditionalExpression.bind (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:5674:8) at eachChild.child (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:5674:34) at keys.forEach.key (C:\SourceCodeNew\IndConsoleSvcProxy\node_modules\rollup\dist\rollup.js:5685:5)
This SO post suggesting change the import to import moment from 'moment' but it doesn’t work for me because I hit another error Module '"C:/SourceCodeNew/IndConsoleSvcProxy/node_modules/moment/moment"' has no default export.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (1 by maintainers)
Commits related to this issue
- refactor(Datepicker): update core deps, move build to rollup — committed to aurelia/ux by bigopon 6 years ago
- fix #226 a workaround fix similar to https://github.com/jvandemo/generator-angular2-library/issues/221#issuecomment-355945207 — committed to Ponkhy/uptime-kuma by louislam 3 years ago
- fix #226 a workaround fix similar to https://github.com/jvandemo/generator-angular2-library/issues/221#issuecomment-355945207 — committed to louislam/uptime-kuma by louislam 3 years ago
My workarround:
😃
FWIW, I ran into a similar problem, and solved it by adding
to my
tsconfig.json. That lets you justimport library from "library"from CommonJS libraries, instead ofimport * as library from 'library', and seems to be a little more forgiving to libraries that otherwise give Typescript troubleNeed to add this to tsconfig.es5.json “allowSyntheticDefaultImports”: true
With Angular 6 this workaround no longer works. Why is moment such a pain in the butt to work with in Angular applications!
import * as moment_ from ‘moment’; const moment = moment_;
Cannot call a namespace (‘moment_’) Error: Cannot call a namespace (‘moment_’)
edit: I retract my statement. After testing in a fresh project everything worked fine.
This workaround worked for us with Angular 6, but now broken again in Angular 7.
On a project I’m working on we had luck importing ‘moment’ like this:
import moment, {Moment} from 'moment';Tests run fine, and the command
ng build <project>builds fine.The setup we’re working in is as follows:
this same issue pops up with
import * as format from 'date-fns/format';and the solution is the same:What is special about moment and date-fns that typescript chokes like this? Like someone said earlier, I thought TS was a superset of JS?
Hi @logic01 , for me it’s working with v6, here. What versions are you using? (ng -v)
@Ismaestro @logic01 — Thank you for the update 👍
👍 Yes moment save my days when I have to work with dates…
I’m having the same issue as @KissBalazs with Angular 7