0

I created a new Next.js project using the command: npx create-next-app@latest

The project installs without problems. The created package.json:

{
  "name": "test_4",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev --turbopack",
    "build": "next build --turbopack",
    "start": "next start"
  },
  "dependencies": {
    "react": "19.1.0",
    "react-dom": "19.1.0",
    "next": "15.5.4"
  },
  "devDependencies": {
    "typescript": "^5",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "@tailwindcss/postcss": "^4",
    "tailwindcss": "^4"
  }
}

But when I try to run the test server: npm run build I get an error:

> [email protected] build
> next build --turbopack

   ▲ Next.js 15.5.5 (Turbopack)

   Creating an optimized production build ...
 ✓ Finished writing to disk in 24ms
 ✓ Compiled successfully in 1915ms
 ✓ Linting and checking validity of types    
 ✓ Collecting page data    
[TypeError: Cannot read properties of null (reading 'useContext')]
Error occurred prerendering page "/404". Read more: https://nextjs.org/docs/messages/prerender-error
[TypeError: Cannot read properties of null (reading 'useContext')]
Export encountered an error on /_error: /404, exiting the build.
 ⨯ Next.js build worker exited with code: 1 and signal: null

I tried running the command without --turbopack: next dev I tried clearing the npm cache: npm cache clean --force I tried completely deleting the _npx cache folder I tried reinstalling the dependencies I tried to create 404 (not-found.tsx) and 500 (error.tsx) pages

But nothing helped

what could be the problem? why won't my project build?

4
  • Not 100% sure what the issue is atm, but your next version in your package.json is version 15.5.4, 15.5.5 is the latest. It looks like it says 15.5.5 in the build output oddly enough. Try changing the version to 15.5.5 in your package.json and/or make sure you don't have next installed globally. Commented Oct 16 at 5:53
  • Try changing your package.json to: "next": "15.5.5" Commented Oct 16 at 7:21
  • I tried updating Next.js to version 15.5.5 and reinstalled the dependencies: npm I but nothing helped, the error persists Commented Oct 16 at 12:38
  • This question is similar to: "next build" failing due to useContext error. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 17 at 19:54

1 Answer 1

0

UPDATE:

Next doesn't support using the global version, you need to use the local per project version. i.e. you're never suppose to use next <command> such as next dev or next build, you should always be using npm run dev and npm run build etc. in a project that has next installed as a dependency of that project.


If you're seeing an error message like:

Command 'next' not found, but can be installed with:
sudo apt install mailutils-mh  # version 1:3.16-1build1, or
sudo apt install mmh           # version 0.4-6
sudo apt install nmh           # version 1.8-1

then that means the global next command was attempted to be used (such as next dev or next build), which you don't want to do, but it can't be found/isn't installed globally (which it shouldn't be). If you see an error message: like:

> [email protected] dev
> next dev --turbopack

sh: 1: next: not found

then that indicates that the local project npm dependency of next was attempted to be used via the command npm run dev in this case (hence the 2nd line of the output) which is what you're suppose to do, but it can't be found/isn't installed as dependency of the project. If that's the case then that means it just needs to be installed, npm i next, and it should work after that. Note that running npx create-next-app should install next automatically as a dependency.


There's also this question asked here where someone was experiencing the same exact issue you're facing. However the cause of their issue was that they were using the global command next build instead of npm run build which doesn't seem to be the case here, but I have to ask/confirm:

  1. you are using npm run build and npm run dev and not next dev and next build.
  2. When you run npm -g list, next isn't listed as a globally installed dependency. If it is try uninstalling it npm -g uninstall next and then build/run the project again and if that doesn't work try creating a fresh next project after making sure it's not installed globally then try building/running the project again.

While it appears that you're not attempting to use the global next command, the issue you're facing and the differing next version in the output vs package.json is all inline with using next globally. In addition to that, a freshly created next project works fine on my system and because I don't have next installed as a global dependency, I can't even run the command next dev without getting a Command 'next' not found... error message.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, you are right, I am using the global version of Next.js. This is because when I install the local version of Next.js, I also get the error Command 'next' not found... I would like to use the local version, but then the project cannot even be started in dev mode.
Ah, well there's the problem. Next doesn't support using the global version, you need to use the local per project version. i.e. you're never suppose to use next <command> such as next dev or next build, you should always be using npm run dev and npm run build in a project that has next installed as a dependency of that project.
I uninstalled the global Next.js: npm uninstall -g next Now Next.js is installed locally in my project Now when I run the local server: npm run dev I get an error: Command 'next' not found... Why does this error occur? How can I fix it?
I can't respond directly to your reply where you said you uninstalled next globally and that you're still getting the Command 'next' not found... error. That shouldn't be the error message you're seeing unless it's still trying to be run globally via next <command>. Have you tried creating a new next project since uninstalling it globally and then doing npm run dev? I updated my answer to show more details and elaborate further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.