Cannot find module 'date-fns' error [Solved]
The “Cannot find module ‘date-fns’” error occurs due to one of the following reasons:
-
The
date-fnsmodule is not installed in the correct project directory. -
Using the module without installing it in the project.
-
Installed the moudle in a global context and trying to access in project local context.
To solve the error “Cannot find module ‘date-fns’”, open the project root folder in your terminal and run the following command to install the date-fns module.
npm install date-fnsNow, you can import and use the date-fns module in your project like this:
import { format } from 'date-fns'
format(new Date(), "MM/dd/yyyy") //=> "10/25/2022"If you want to install a particular version of ‘date-fns’ module, you can use the following command.
npm install date-fns@2.29.3If you’re still facing the error, then follow the below steps to resolve it.
- Remove the
node_modulesfolder andpackage-lock.jsonfile, inside your project directory by using the below command.
rm-rf node_modules package-lock.jsonor you can remove it manually by right-clicking on it and select the delete option.
- Clear the npm cache.
npm clean cache --force- Re-install the node modules again by running the
npm installcommand.
Verify now if you’ve installed it correctly or not by opening the package.json file and check for date-fns module in the dependencies object.
"dependencies": {
//...
"date-fns": "^2.29.3",
//.. other packages goes here
}Conclusion
To solve the can’t find module date-fns error, install the date-fns module in your project directory by running the npm install date-fns command and restart your development server.


