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

TypeError: moment__WEBPACK_IMPORTED_MODULE_15___default(...)(...).tz is not a function when upgrade from moment2.29.1 to moment 2.29.4 #6087

Closed
galaxy-dot opened this issue Jan 13, 2023 · 2 comments

Comments

@galaxy-dot
Copy link

Describe the bug
I tried to upgrade moment from 2.29.1 to 2.29.4 to fix ReDos vunl (I think this is the same reason why 2.29.4 is release)

To Reproduce
Steps to reproduce the behavior:

  1. In package.json, "dependencies": { ..., moment: "^2.27.0", ...}
  2. In Terminal, type "yarn upgrade moment@2.29.4" to bump version
  3. Can see 2.29.4 is download in node_modules and yarn.lock is updated automatically
  4. Tried to build and start the app, failed.
  5. Tried to console.log error, see TypeError: moment__WEBPACK_IMPORTED_MODULE_15___default(...)(...).tz is not a function

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
with 2.29.1, moment() object has 7 properties, see screenshot
Screen Shot 2023-01-13 at 4 40 09 pm

with 2.29.4, moment() object has 6 properties, does not have the last '_z' property.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information if applicable):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. firefox, safari]
  • Version [e.g. 22]

Moment-specific environment

  • The time zone setting of the machine the code is running on
  • The time and date at which the code was run
  • Other libraries in use (TypeScript, Immutable.js, etc)

Please run the following code in your environment and include the output:

console.log((new Date()).toString()) //Fri Jan 13 2023 17:08:38 GMT+1100 (Australian Eastern Daylight Time)

  console.log((new Date()).toLocaleString()) //13/01/2023, 17:08:38
  console.log((new Date()).getTimezoneOffset()) //-660
  console.log(navigator.userAgent); //Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
  console.log(moment.version); // 2.29.4

Additional context
Code snippet

// helper.js

import moment from 'moment';

export const currentTZ = () => {
  // console.log(Object.keys(moment()));   // with 2.29.1, this works; the object has 7 properties, last one is "_z". with 2.29.4, this does not work; the object has 6 properties, does not have "_z" property.

  return moment().tz("Australia/Sydney")
    .format('Z') === '+11:00' ? 'AEDT' : 'AEST';
}

Any help is appreciated!

@gilmoreorless
Copy link
Member

Your code snippet doesn't seem to be importing moment-timezone anywhere, so it makes sense that .tz isn't available.

That said, if it worked before, I suspect there's a separate piece of code in your project that imports moment-timezone, which in turn mutates the underlying (and shared) moment object. With the upgrade to the newer moment version, that reference link might now be broken. See moment/moment-timezone#982 (comment) for a more detailed explanation of how there can be different versions of moment in the same project.

@galaxy-dot
Copy link
Author

Thanks @gilmoreorless , it's very detailed explanations and great direction. Appreciated your help!

I am able to fix the issue by following steps:

  1. In package.json -> "dependencies" -> remove removed "moment": "^2.27.0"; keep "moment-timezone": "^0.5.33"; then de-duplicate version, as suggested in How to fix it section in this thread. I ran below cmds in sequence.
$ yarn remove moment
$ npx yarn-deduplicate --packages moment
$ yarn install
  1. In helper.js, updated the OLD imports to NEW imports
// OLD imports in helper.js 

import moment from 'moment';  // <- removed this import
import momentTimezone from 'moment-timezone';
// NEW imports in helper.js

import moment from 'moment-timezone'; // <- update to only use one import from 'moment-timezone'
  1. All unit tests are passing and build is successful now.

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

2 participants