kinx
kinx

Reputation: 493

Error when building next.js app: Can't resolve 'next/head'

I am getting the below error when doing next build in my next.js app, after it's been working fine with just next (or npm run dev).

The error:

$ 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)

I started my project by cloning Material UI's next.js example and just use that as it seems updated:

https://github.com/mui-org/material-ui/tree/master/examples/nextjs

However, thinking I just may of have an outdated or broken build, I re-cloned the next.js example and did next build as well, without modifying any files and I get the same error.

How can I fix this?

Upvotes: 1

Views: 15195

Answers (3)

Peter W
Peter W

Reputation: 1494

I ran into a similar problem when I included my nextjs app in a yarn workspace. I got around the issue by deleting the following directories in my nextjs app:

  • node_modules
  • .next
  • build

After removing them I ran yarn from the repo directory and started the nextjs app back up and it was working again.

Upvotes: 0

azeem
azeem

Reputation: 361

The solution to this problem is to Just rerun these two commands again then your project will successfully Run. I also faced this problem but I got the solution.

  1. npm i
  2. yarn install

Upvotes: 0

Jorge Barreto
Jorge Barreto

Reputation: 188

I just cloned that next.js example and it worked both with npm run dev, as well as npm run build->npm run start.

Judging by your error, I would check that "react-dom": "latest" is present in your package.json's dependencies. Did you run npm install after cloning the example?

Upvotes: 0

Related Questions