moment-timezone icon indicating copy to clipboard operation
moment-timezone copied to clipboard

Error while using moment

Open sushant23 opened this issue 8 years ago • 75 comments

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)

sushant23 avatar Feb 23 '17 10:02 sushant23

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

sushant23 avatar Feb 25 '17 18:02 sushant23

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?

BipinBhandari avatar Feb 27 '17 05:02 BipinBhandari

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.

peterjcaulfield avatar Mar 07 '17 11:03 peterjcaulfield

Same issue here, I hope this get addressed any time soon 😞

lmammino avatar Mar 15 '17 09:03 lmammino

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?

butterflyhug avatar Apr 07 '17 14:04 butterflyhug

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.

maggiepint avatar Apr 07 '17 14:04 maggiepint

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.

brinley avatar Apr 08 '17 09:04 brinley

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.

butterflyhug avatar Apr 08 '17 17:04 butterflyhug

Seeing this problem as well.

Using the following: "moment": "2.6.0", "moment-timezone": "0.5.13",

NathanBWaters avatar May 02 '17 22:05 NathanBWaters

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.

soundstripe avatar Jun 28 '17 15:06 soundstripe

same here Please if anyone have workaround please post

msonowal avatar Jul 22 '17 08:07 msonowal

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';

gsdnano avatar Jul 26 '17 23:07 gsdnano

Same. in fact, this works for me as well:

import moment from 'moment-timezone';

carldunham avatar Jul 27 '17 03:07 carldunham

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/

rushglen avatar Aug 31 '17 16:08 rushglen

This hack still doesn't work for me, any other suggestion?

pedro-teixeira avatar Sep 01 '17 15:09 pedro-teixeira

This doesn't work for me either? Anyone figures it out yet?

henrycity avatar Sep 08 '17 11:09 henrycity

https://github.com/moment/moment-timezone/pull/476 this one seems to work like @BipinBhandari suggested.

henrycity avatar Sep 12 '17 10:09 henrycity

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'

opavader avatar Sep 26 '17 13:09 opavader

Same issue 2.19.0 Seems like can not get version with moment.version But moment.default.version can work

TakanashiOuken avatar Oct 10 '17 14:10 TakanashiOuken

Latest version also seems to break for us using Webpack. Everything was fine earlier.

SargoDarya avatar Oct 10 '17 16:10 SargoDarya

I have same problem, in my case it resolve by using moment 2.18.1, not 2.19.

Etrimus avatar Oct 10 '17 17:10 Etrimus

@Etrimus have you fix by using moment 2.18.1?

dharmesh03 avatar Oct 10 '17 17:10 dharmesh03

@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.

Etrimus avatar Oct 10 '17 17:10 Etrimus

Was this breaking change released today? Was working fine last night, redeployed today and got this error. Spent hours debugging it

danjohnson95 avatar Oct 10 '17 17:10 danjohnson95

@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.

Etrimus avatar Oct 10 '17 17:10 Etrimus

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 avatar Oct 10 '17 17:10 zbennett10

@zbennett10 couldn’t agree more. Will be a wake up call for me too

danjohnson95 avatar Oct 10 '17 17:10 danjohnson95

See https://github.com/moment/moment/issues/4216 for info about problems happening due to 2.19.0 release.

maggiepint avatar Oct 10 '17 18:10 maggiepint

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",

mnpenner avatar Oct 11 '17 02:10 mnpenner