Cannot find module 'date-fns' error

avatar

Borislav Hadzhiev

Last updated: Mar 27, 2022

banner

Photo from Unsplash

Cannot find module 'date-fns' error #

To solve the error "Cannot find module 'date-fns'", make sure to install the date-fns package by opening your terminal in your project's root directory and running the following command: npm i date-fns and restart your IDE and development server if necessary.

index.js
// ⛔️ Error Cannot find module 'date-fns' // [ERR_MODULE_NOT_FOUND]: Cannot find package 'date-fns' // imported from bobbyhadz-js/index.js import {format} from 'date-fns'; const d1 = new Date('Nov 24, 22 13:20:18'); const result = format(d1, 'yyyy-MM-dd'); console.log(result);

Open your terminal in your project's root directory (where your package.json file is located) and run the following commands:

shell
npm install date-fns

This will add the date-fns package to the dependencies of your project.

Now you should be able to import and use the date-fns package like so:

index.js
import {format} from 'date-fns'; const d1 = new Date('Nov 24, 22 13:20:18'); const result = format(d1, 'yyyy-MM-dd'); console.log(result); // 👉️ "2022-11-24"

If you use TypeScript and are getting the error "Cannot find module 'date-fns' or its corresponding type declarations", try installing the @types/date-fns as well.

shell
npm install --save-dev @types/dev-fns

If your error has not been resolved, open your tsconfig.json file and make sure the types array contains the string node.

tsconfig.json
{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the date-fns module.

If the "Cannot find module 'date-fns'" error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

shell
# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.

If you're still getting the error, open your package.json file and make sure it contains the date-fns package in the dependencies object.

package.json
{ // ... rest "dependencies": { "date-fns": "^2.28.0", }, }

You can try to manually add the line and re-run npm install.

shell
npm install

Or install the latest version of the package:

shell
npm install date-fns@latest

Conclusion #

To solve the error "Cannot find module 'date-fns'", make sure to install the date-fns package by opening your terminal in your project's root directory and running the following command: npm i date-fns and restart your IDE and development server if necessary.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.