DEV Community

Ajeet Singh Raina
Ajeet Singh Raina

Posted on

"sh: next: command not found" in Next.js Development? Here's the Fix!

Ever run npm run dev in your Next.js project only to be greeted by the confusing sh: next: command not found error? Don't worry, this is a common hurdle faced by developers, and it's easily fixable.

This error pops up because the next command isn't recognized by your terminal. It could be due to two reasons:

  • Next.js Installed Locally: Most likely, Next.js is installed as a project dependency, not globally on your system.
  • Missing Script: Your package.json might be missing the script that triggers the next dev command.

Let's explore solutions for both scenarios:

Solution 1: Running Next.js Locally

If you prefer to keep Next.js specific to your project, follow these steps:

1. Utilize the Full Command

Since Next.js is a local dependency, you can directly run the development server using the full command:

npm run dev
Enter fullscreen mode Exit fullscreen mode

This should trigger the script defined in your package.json (assuming it includes next dev).

2. Verify package.json Script:

Open your project's package.json and check for the "scripts" section. Make sure it contains the following script:

JSON
"scripts": {
  "dev": "next dev"
}
Enter fullscreen mode Exit fullscreen mode

If missing, add this script and try npm run dev again.

Solution 2: Installing Next.js Globally (Optional)

This approach allows you to use next commands from any directory in your terminal. However, it's generally recommended to keep Next.js local to avoid version conflicts with other projects. Here's how to do it (use with caution):

Global Installation:

npm install -g next
Enter fullscreen mode Exit fullscreen mode

This installs Next.js globally on your system.

Run Next.js Commands:

Now you can directly use commands like next dev or next build from any directory.

By following these solutions, you should be able to resolve the "sh: next: command not found" error and successfully launch your Next.js development server. Remember, keeping Next.js local is preferred for better project isolation.

Happy Coding!

Top comments (0)