[英]Error when building next.js app: Can't resolve 'next/head'
在我的 next.js 應用程序中進行next build
時,我收到以下錯誤,在它與next
(或npm run dev
)正常工作之后。
錯誤:
$ next build
The module 'react-dom' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install --save react-dom'
Creating an optimized production build ...
> Using external babel configuration
> Location: "../.babelrc"
Failed to compile.
./pages/_app.js
Module not found: Can't resolve 'next/head' in '../pages'
> Build error occurred
Error: > Build failed because of webpack errors
at build (.../AppData/Roaming/npm/node_modules/next/dist/build/index.js:7:847)
我通過克隆 Material UI 的 next.js 示例開始了我的項目,並使用它,因為它似乎已更新:
https://github.com/mui-org/material-ui/tree/master/examples/nextjs
但是,我認為我可能只是有一個過時或損壞的構建,我重新克隆了 next.js 示例並進行了next build
,沒有修改任何文件,我得到了同樣的錯誤。
我怎樣才能解決這個問題?
我剛剛克隆了 next.js 示例,它與 npm run dev 以及 npm run build->npm run start 一起工作。
根據您的錯誤判斷,我會檢查您的package.json
的依賴項中是否存在"react-dom": "latest"
。 克隆示例后是否運行了 npm install ?
解決這個問題的方法是再次重新運行這兩個命令,然后您的項目將成功運行。 我也遇到了這個問題,但我得到了解決方案。
當我將 nextjs 應用程序包含在 yarn 工作區中時,我遇到了類似的問題。 我通過在我的 nextjs 應用程序中刪除以下目錄解決了這個問題:
刪除它們后,我從 repo 目錄運行yarn
並啟動 nextjs 應用程序備份,它再次工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.