在仅使用next build
(或next
)正常运行后,在next.js应用中执行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
,而没有修改任何文件,并且得到了相同的错误。
我该如何解决?
答案 0 :(得分:0)
我刚刚克隆了next.js示例,它可以与npm run dev以及npm run build-> npm run start一起使用。
从您的错误判断,我将检查"react-dom": "latest"
的依赖项中是否存在package.json
。克隆示例后是否运行npm install?
答案 1 :(得分:0)
此问题的解决方案是再次重新运行这两个命令,则您的项目将成功运行。我也遇到了这个问题,但是我找到了解决方案。
答案 2 :(得分:0)
当我在 yarn 工作区中包含我的 nextjs 应用程序时,我遇到了类似的问题。我通过删除 nextjs 应用程序中的以下目录解决了这个问题:
删除它们后,我从 repo 目录运行 yarn
并启动 nextjs 应用程序备份,它再次运行。