moment-timezone
moment-timezone copied to clipboard
Error while using moment
I am getting the following error while trying to use this library in my project
Uncaught (in promise) TypeError: Cannot read property 'split' of undefined at Object.eval (moment-timezone.js:34) at eval (moment-timezone.js:12) at eval (moment-timezone.js:18) at Object../node_modules/moment-timezone/moment-timezone.js (reactBoilerplateDeps.dll.js:4931) at webpack_require (reactBoilerplateDeps.dll.js:21) at eval (index.js:1) at Object../node_modules/moment-timezone/index.js (reactBoilerplateDeps.dll.js:4924) at webpack_require (reactBoilerplateDeps.dll.js:21) at eval (eval at 42 (36.chunk.js:83),
:1:42) at Object.42 (36.chunk.js:83)
Hi I was having the same issue make sure you are using at least moment 2.6.0 as in the docs
I am currently using latest version i.e. 2.17.1 and I am still getting the same error
This issue seems to be fixed by settings moment=moment.default
in moment-timezone.js line 20
}(this, function (moment) {
"use strict";
moment=moment.default;
Might be issue with import. Can anyone fix?
I'm also experiencing this issue using es6 imports.
dep versions:
"moment": "2.17.1",
"moment-timezone": "^0.5.11",
code:
import moment from 'moment';
import 'moment-timezone';
moment.tz.setDefault('Europe/London');
Produces:
Uncaught TypeError: Cannot read property 'split' of undefined
at Object.eval (webpack:///./~/moment-timezone/moment-timezone.js?:37)
Not 💯 on whether or not I am importing moment-timezone
correctly but I couldn't find any documentation on how to handle this.
Same issue here, I hope this get addressed any time soon 😞
I'm a little confused about what is happening here. It looks like y'all are having trouble using ES6 import syntax with webpack. What I don't understand is how you're importing moment as an ES6 module in the first place -- there's absolutely nothing in our packaging that would allow you to import moment using ES6 syntax and get anything other than the normal browser global. Something is rewriting our packaging in order to achieve this result -- presumably webpack, but I've never used it so I don't really know how it works.
I'm inherently skeptical about adding code to moment-timezone to support a module system that moment and moment-timezone do not support. Many webpack users are experiencing pain with Moment's locales now, which is fundamentally rooted in the fact that we built our UMD packaging under the assumption that everyone using CommonJS is using Node (which was generally true at the time). So this time around, I really don't want to assume that everyone using ES6 is using webpack or other current-generation tools. In other words, I'd like to be convinced that this solution will always be the right thing to do to support ES6 modules, even in a future where another packaging tool decides to translate our UMD setup differently, or even in a future where Moment supports ES6 modules natively.
Could someone please explain in more detail what webpack is doing here, and why #476 would be a reasonable solution to include in moment-timezone?
For what it's worth, I'm starting a webpack project right now (like, on day 1 here). I'll pull in moment since I tend to always do so anyways and see what I can make of it.
Essentially the issue we have is at present is if we do something like
import moment from 'moment'
import 'moment-timezone'
or
import moment from 'moment-timezone'
We get the exception trying to split moment.version which is undefined
Importing moment works fine but without the timezone functionality such as
import moment from 'moment'
I trust that the best way is to properly support ES6 modules but https://github.com/moment/moment-timezone/pull/476 is very non-disruptive PR as moment.version would error if its undefined.
Sorry, I guess I was unclear. I understood that moment-timezone is getting a wrapped copy of moment in your environment, and that you want it to be unwrapped so that the plugin works correctly. What I don't understand is why moment is getting wrapped in your environment, nor do I know if there is good reason to expect this wrapping behavior to be consistent with other environments (including future tools) that implement the same goals. Without that information, I don't personally feel confident that #476 will continue to be non-disruptive as the Javascript ecosystem continues to evolve over time.
Put another way, I'm not sure whether this would be properly fixed in moment-timezone or in webpack. Absent other information, I think it sounds suspiciously like webpack might be breaking moment's perfectly-valid AMD modules in the process of translating them to ES6 modules. If that is actually what is happening, then I would prefer to have webpack fix this instead of moment. But if that's not what is going on, or if we should expect the same behavior if/when we decide to natively support ES6 modules ourselves, then we should carefully vet and move forward with a solution in moment. (And then we should share that solution with other projects that also use UMD, since they will presumably face similar issues.)
tl;dr -- Yes, I want to get your build working -- but I want to make sure we do that in a way that avoids similar issues in the future, in moment and ideally also in other libraries that use the same packaging structure. I hope we'll take the time required to sort out exactly what is going on, in order to accomplish that larger goal.
Seeing this problem as well.
Using the following: "moment": "2.6.0", "moment-timezone": "0.5.13",
It looks like this is happening because of a breaking change made in the babel6 transpiler (almost always required in webpack).
Here's the issue on babel's GitHub.
Here's a related Stack Overflow link.
Here is a babel plugin that may help.
same here Please if anyone have workaround please post
The link above to https://github.com/59naga/babel-plugin-add-module-exports plus the following worked for me.
import moment from 'moment';
import 'moment-timezone';
Same. in fact, this works for me as well:
import moment from 'moment-timezone';
If using in browser you have to use the moment.js file as well.... The moments homepage where you can download moment-timezone.js and moment-timezone-with-data.js does not make it clear you also need moment.js............
`
`
console.log(a.format()); `
moment.js is available here: https://cdnjs.com/libraries/moment.js
Info found here: https://momentjs.com/docs/
This hack still doesn't work for me, any other suggestion?
This doesn't work for me either? Anyone figures it out yet?
https://github.com/moment/moment-timezone/pull/476 this one seems to work like @BipinBhandari suggested.
As a temporary workaround, I have create an updated fork patched with https://github.com/moment/moment-timezone/pull/476. You can use it for now with
yarn add 'https://github.com/archsaber/moment-timezone.git#develop'
Same issue 2.19.0
Seems like can not get version with moment.version
But moment.default.version
can work
Latest version also seems to break for us using Webpack. Everything was fine earlier.
I have same problem, in my case it resolve by using moment 2.18.1
, not 2.19
.
@Etrimus have you fix by using moment 2.18.1?
@dharmesh03 Yes, I fixed it just now in my project (Angular 4, Webpack). In My case moment
is a dependency of other npm-package, but I finded version of moment
by using command npm ls
, and I saw 2.19.0
. So, I just set concrete version in My package.json "moment": "2.18.1"
and It work.
Was this breaking change released today? Was working fine last night, redeployed today and got this error. Spent hours debugging it
@danjohnson95 In my opinion It is 2.19.0
release of moment
, not moment-timezone
, released some hours ago - https://github.com/moment/moment/releases/tag/2.19.0, so You right - everything worked yesterday.
I can confirm what @Etrimus is saying - this was a wake-up call for me to rely on the '~' in front of my package version instead of the '^'
@zbennett10 couldn’t agree more. Will be a wake up call for me too
See https://github.com/moment/moment/issues/4216 for info about problems happening due to 2.19.0 release.
I couldn't for the life of me get yarn to downgrade the moment.js version, even after deleting node_modules
, yarn.lock
and yarn cache clean
. Switched back to npm and successfully downgraded.
The versions that work for me are:
"moment": "~2.18.1",
"moment-timezone": "~0.5.13",