Search code examples
typescriptmomentjsantd

How to Upgrade Momentjs in AntDesign 3.x


Upgrade guide in AntD site is broken. How can you update the moment dependency of AntDesign 3.x to latest version of moment?

AntD's DatePicker's RangePicker value prop is tied up to its own momentjs. So, if your app needs to use a higher version of moment, Tyepscript will complain.


Solution

  • In your package.json, add the resolutions

      "resolutions": {
        "moment": "<target or latest version>",
        "moment-timezone": "<target or latest version>"
      },
      "dependencies": {
        ...
        "moment": "<latest>",
        "moment-timezone": "<latest>",
        ...
      }
    

    What this do, is it tells the libraries that have their own momentjs as subpackage (but old) to use the latest momentjs instead of their own.

    You can verify with console.log(moment.version).

    But this does not assure full compatibility. If the version diff is major. Like 3.0.0 vs 2.0.0, there could (surely) be breaking changes. Minor/patch diff should work just fine, but better verify by testing.

    Some good explanations here

    https://medium.com/learnwithrahul/understanding-npm-dependency-resolution-84a24180901b

    https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/