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

moment has no default member #4397

Closed
luckylooke opened this issue Jan 9, 2018 · 4 comments
Closed

moment has no default member #4397

luckylooke opened this issue Jan 9, 2018 · 4 comments

Comments

@luckylooke
Copy link

luckylooke commented Jan 9, 2018

Description of the Issue and Steps to Reproduce:
Trying to implement instance locale in typescript project
https://momentjs.com/docs/#/i18n/instance-locale/
but when I import moment from 'moment' I get error:
moment has no default export

purpose:
I need custom forms (shorten, like 'sec' instead 'seconds') of humanized strings in some parts of application, so I would like to use two locales in one view.

Environment:
typescript project

Other information that may be helpful:
TypeScript, Angular4

If you are reporting an issue, please run the following code in the environment you are using and include the output:

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)
Tue Jan 09 2018 15:46:46 GMT+0100 (CET)
VM12667:2 09/01/2018, 15:46:46
VM12667:3 -60
VM12667:4 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
VM12667:5 Uncaught ReferenceError: moment is not defined
    at <anonymous>:5:13
import * as moment from 'moment';
console.log('version', moment.version);
version 2.20.1

I would like to prevent the import * as moment from 'moment'; solution since it blocks three shaking in bundling

Many thanks! :)

@luckylooke
Copy link
Author

luckylooke commented Jan 17, 2018

RTFM 😂

Note: If you have trouble importing moment, try adding 
"allowSyntheticDefaultImports": true in compilerOptions 
in your tsconfig.json file and then use the syntax

https://momentjs.com/docs/#/use-it/typescript/

@evert
Copy link

evert commented Apr 15, 2019

Is there a chance that this will be resolved in a way where allowSyntheticDefaultImport is no longer required? The issue with requiring it is that it requires everyone who uses moment to also have this setting on.

By not requiring this setting, you allow moment to be easily used in projects that have their own preference for allowSyntheticDefaultImport, but now that setting is effectively forced on everyone.

@owenallenaz
Copy link

Just chiming in here that this is a really, truly, obnoxious problem. Since moment has chosen not to respect the official spec, it means that some libraries pull in via import * as moment from "moment" while other libraries run via import defaultMoment from "moment". So, when my project attempts to load both, I just cannot get the right settings to get rollup to properly compile it. I've tried nearly every permutation and combination of allowSyntheticDefaultImports : true, esModuleInterop : true, moduleResolution : "node" and skipLibCheck : true and I cannot get a permutation that works correctly. One solution will allow one library with one import format to work, but it fails in the other.

There's a reason that spec's exist, ans that's to create consistency. Is there no way that moment could have a moment-es variant or something which can be properly imported, so that way people that want to follow the spec can just follow the spec, allowing the JS toolchain to work as expected, rather than all the hacks?

@pokono
Copy link

pokono commented Jul 6, 2020

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants