找不到模块:无法解析“@date-io/date-fns”

找不到模块:无法解析“@date-io/date-fns”

Module not found: Can’t resolve ‘@date-io/date-fns’

要解决错误“找不到模块:错误:无法解析’@date-io/date-fns’”,请确保通过在项目根目录中打开终端并运行命令
npm install @date-io/date-fns date-fns并重新启动开发来安装软件包服务器。

找不到模块无法解析日期 io 日期 fns

在项目的根目录(package.json
文件所在的位置)中打开终端并运行以下命令:

# 👇️ 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 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

Conclusion #

要解决错误“找不到模块:错误:无法解析’@date-io/date-fns’”,请确保通过在项目根目录中打开终端并运行命令
npm install @date-io/date-fns date-fns并重新启动开发来安装软件包服务器。