Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Build fails because react-moment cannot find the moment package

ErrorModule not found: Can't resolve 'moment'

Short answer

react-moment does not bring the moment library with it. The README lists moment as a peer dependency, so your own project has to install it. Add moment to package.json, reinstall, and restart the dev server. The timezone and duration props need extra packages too: moment-timezone and moment-duration-format.

  • Applies to react-moment (current README), React 18 and 19, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List what is actually installed. If moment is missing from the tree, the first cause below is yours.

    npm ls moment react-moment
  2. 2Match what you see, then jump to its fix

    1. 1npm ls shows react-moment but no moment entry (or 'empty')moment was never installed in your projectFix
    2. 2The failing import is moment-timezone, not moment, and only components with a tz prop use itYou use the timezone prop without moment-timezoneFix
    3. 3The unresolved package is moment-duration-formatYou use duration or durationFromNow without moment-duration-formatFix
    4. 4moment is in package.json but missing from node_modulesnpm skipped peer dependencies on this installFix

What does the error look like?

  • Module not found: Can't resolve 'moment'
  • Can't resolve 'moment' in 'node_modules/react-moment/dist'

Cause 1 of 4

moment was never installed in your project

1Check

Open package.json and look for moment under dependencies. The react-moment README names moment as a peer dependency, which means the app that uses it must provide it.

grep -n '"moment"' package.json

2Fix

Install both packages together, exactly as the README shows.

npm install --save moment react-moment

3Verify

The tree now lists moment, and the build no longer reports it.

npm ls moment
npm run build

Confirmed by github.com

Did this fix it?

Cause 2 of 4

You use the timezone prop without moment-timezone

1Check

Search your code for the tz prop. The README says the moment-timezone package is required for the timezone related functions.

grep -rn "tz=" src/

2Fix

Install moment-timezone and import it once at the top of the file or app entry, as the README shows.

npm install --save moment-timezone

// index.js or the file that renders <Moment tz=...>
import 'moment-timezone';

3Verify

Restart the dev server and render the component with its tz prop.

npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

You use duration or durationFromNow without moment-duration-format

1Check

Look for the duration or durationFromNow props. The README says moment-duration-format is required for those two props and is an optional peer dependency.

grep -rnE "duration(FromNow)?=" src/

2Fix

Install it only if you use those props.

npm install --save moment-duration-format

3Verify

Rebuild and load the page that shows the duration.

npm run build

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

npm skipped peer dependencies on this install

1Check

The legacy-peer-deps setting makes npm ignore peerDependencies entirely, and --omit=peer stops them being unpacked to disk. Check .npmrc and your install command for either.

npm config get legacy-peer-deps
npm config get omit

2Fix

Remove the setting for this project, or list moment as a direct dependency of your app (as in the first cause) so it is installed whatever the peer setting is.

npm install --save moment

3Verify

Delete node_modules and reinstall, then check the tree.

rm -rf node_modules
npm install
npm ls moment

Confirmed by docs.npmjs.com

Did this fix it?

How do I stop it coming back?

  • Install moment as a direct dependency whenever you add react-moment.
  • Read the Installing section of a package README before adding it; it names peer dependencies.
  • Keep moment-timezone and moment-duration-format only if you use the props that need them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.