Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call a namespace ('moment') #221

Closed
sheldonyss opened this issue Oct 16, 2017 · 13 comments

Comments

Projects
None yet
8 participants
@sheldonyss
Copy link

commented Oct 16, 2017

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.

@sheldonyss

This comment has been minimized.

Copy link
Author

commented Oct 16, 2017

Need to add this to tsconfig.es5.json "allowSyntheticDefaultImports": true

@Ismaestro

This comment has been minimized.

Copy link

commented Jan 8, 2018

Same problem, @sheldonyss that does not works!

@Ismaestro

This comment has been minimized.

Copy link

commented Jan 8, 2018

My workarround:

import * as moment_ from 'moment';

const moment = moment_;

:)

@dave0688

This comment has been minimized.

Copy link

commented Jan 19, 2018

Wow, thanks @Ismaestro.
Really wondering how you found that out... :D

@Ismaestro

This comment has been minimized.

Copy link

commented Jan 20, 2018

From here. ;)

@logic01

This comment has been minimized.

Copy link

commented May 8, 2018

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.

@Ismaestro

This comment has been minimized.

Copy link

commented May 8, 2018

Hi @logic01 , for me it's working with v6, here. What versions are you using? (ng -v)

screen shot 2018-05-08 at 18 40 21

@logic01

This comment has been minimized.

Copy link

commented May 9, 2018

You are correct, it does appear to work. After testing in a fresh Angular 6 project + Library project everything worked fine. I must have made a mistake in my upgraded project.

It still seems like moment is the odd man out when it comes to importing but at least it's working. Moment is such a useful library.

@Ismaestro

This comment has been minimized.

Copy link

commented May 9, 2018

👍 Yes moment save my days when I have to work with dates...

@jvandemo

This comment has been minimized.

Copy link
Owner

commented May 9, 2018

@Ismaestro @logic01 — Thank you for the update 👍

@KissBalazs

This comment has been minimized.

Copy link

commented Oct 26, 2018

This workaround worked for us with Angular 6, but now broken again in Angular 7.

@alsoicode

This comment has been minimized.

Copy link

commented Nov 9, 2018

I'm having the same issue as @KissBalazs with Angular 7

@westandy-dcp

This comment has been minimized.

Copy link

commented Dec 6, 2018

this same issue pops up with import * as format from 'date-fns/format'; and the solution is the same:

import * as format_ from 'date-fns/format';
const format = format_;

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?

bhalash added a commit to D4H/api-angular that referenced this issue Jun 6, 2019

Added workaround for referential bug.
See: D4H/decisions-project#3506
See: jvandemo/generator-angular2-library#221

Per the GitHub issue above:

// Error: Cannot call a namespace ('moment') at error (...)
import * as moment from 'moment'

// Works
import * as moment_ from 'moment';
const moment = moment_;

¯\_(ツ)_/¯
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.