Build fails because react-moment cannot find the moment package
Module 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.
Fix it fast
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-moment2Match what you see, then jump to its fix
- 1
npm ls shows react-moment but no moment entry (or 'empty')moment was never installed in your projectFix - 2
The failing import is moment-timezone, not moment, and only components with a tz prop use itYou use the timezone prop without moment-timezoneFix - 3
The unresolved package is moment-duration-formatYou use duration or durationFromNow without moment-duration-formatFix - 4
moment is in package.json but missing from node_modulesnpm skipped peer dependencies on this installFix
- 1
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.json2Fix
Install both packages together, exactly as the README shows.
npm install --save moment react-moment3Verify
The tree now lists moment, and the build no longer reports it.
npm ls moment
npm run buildConfirmed by github.com
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 devConfirmed by raw.githubusercontent.com
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-format3Verify
Rebuild and load the page that shows the duration.
npm run buildConfirmed by raw.githubusercontent.com
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 omit2Fix
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 moment3Verify
Delete node_modules and reinstall, then check the tree.
rm -rf node_modules
npm install
npm ls momentConfirmed by docs.npmjs.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix