Solve - Module not found: Can't resolve '@date-io/date-fns'

avatar

Borislav Hadzhiev

Last updated: Mar 21, 2022

banner

Photo from Unsplash

Solve - Module not found: Can't resolve '@date-io/date-fns' #

To solve the error "Module not found: Error: Can't resolve '@date-io/date-fns'", make sure to install the package by opening your terminal in your project's root directory and running the command npm install @date-io/date-fns date-fns and restart your development server.

module not found cant resolve date io date fns

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

shell
# 👇️ with NPM npm install @date-io/date-fns date-fns # 👇️ if you use Luxon npm install @date-io/luxon luxon # 👇️ if you use TypeScript npm install --save-dev @types/luxon # 👇️ if you use Moment npm install @date-io/moment moment # 👇️ if you use TypeScript npm install --save-dev @types/moment # 👇️ if you use Dayjs npm install @date-io/dayjs dayjs # ---------------------------------------------- # 👇️ with YARN yarn add @date-io/date-fns date-fns # 👇️ if you use Luxon yarn add @date-io/luxon luxon # 👇️ if you use TypeScript yarn add @types/luxon --dev # 👇️ if you use Moment yarn add @date-io/moment moment # 👇️ if you use TypeScript yarn add @types/moment --dev # 👇️ if you use Dayjs yarn add @date-io/dayjs dayjs
You only need to install the luxon, moment and dayjs packages if you import and use them in your project, otherwise only install @date-io/date-fns and date-fns.

The command will add the @date-io/date-fns package to the dependencies of your project.

Make sure to restart your development server and your IDE if necessary. Your dev server won't pick up the changes until you stop it and re-run the npm start command.

You should now be able to import and use the @date-io/date-fns package in your application.

App.js
import DateFnsAdapter from '@date-io/date-fns'; const dateFns = new DateFnsAdapter(); const initialDateFnsDate = dateFns.date('2022-09-24T09:30:00.000Z'); console.log(initialDateFnsDate); const updatedDateFnsDate = dateFns.addDays(initialDateFnsDate, 4); console.log(updatedDateFnsDate);

For more examples refer to the Github page of the date-io package.

If the 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 still persists. VSCode often glitches and a reboot solves things sometimes.

If you're still getting the "Module not found: Error: Can't resolve '@date-io/date-fns'" error, open your package.json file and make sure it contains the @date-io/date-fns package in the dependencies object.

package.json
{ // ... rest "dependencies": { "@date-io/date-fns": "^2.13.1", "date-fns": "^2.28.0", "@date-io/luxon": "^2.13.1", "luxon": "^2.3.1", "@date-io/moment": "^2.13.1", "moment": "^2.29.1", "@date-io/dayjs": "^2.13.1", "dayjs": "^1.11.0", }, }

The @date-io/date-fns module should NOT be globally installed or be in your project's devDependencies, it should be in the dependencies object in your package.json file.

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

shell
npm install

Or install the latest version of the package:

shell
npm install @date-io/date-fns@latest date-fns@latest # 👇️ if you use Luxon npm install @date-io/luxon@latest luxon@latest # 👇️ if you use TypeScript npm install --save-dev @types/luxon@latest # 👇️ if you use Moment npm install @date-io/moment@latest moment@latest # 👇️ if you use TypeScript npm install --save-dev @types/moment@latest # 👇️ if you use Dayjs npm install @date-io/dayjs@latest dayjs@latest
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.