Last updated: Apr 4, 2024
Reading timeยท5 min

The error "'next' is not recognized as an internal or external command, operable program or batch file" occurs when we forget to install the dependencies in a Next.js project.
To solve the error, run npm install react react-dom next to install next
before running npm run dev.

Open your shell in your project's root directory (where your package.json is)
and run the following commands.
# ๐๏ธ if you use npm npm install npm install react react-dom next npm run dev # ๐๏ธ if you use yarn yarn install yarn add react react-dom next yarn dev
Once you run npm install or
yarn install and
install the react, react-dom and next packages, you will be able to issue
the npm run dev command without getting any errors.

node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.# ๐๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install npm run dev
Alternatively, you can create a new Next.js project with the following command.
npx create-next-app@latest
If the error persists, add npm to your PATH environment variable manually.
npm to your PATH environment variableTo add npm to your PATH environment variable:




npm directory is most likely located under
%USERPROFILE%\AppData\Roaming\npm or in other words,
C:\Users\YOUR_USER\AppData\Roaming\npm.%USERPROFILE%\AppData\Roaming\npm # ๐๏ธ same as below (make sure to replace YOUR_USER) C:\Users\YOUR_USER\AppData\Roaming\npm
If you can't find it, run the npm config get prefix command.
npm config get prefix

Add the path to npm and click on "OK" twice to confirm.
Close your Command prompt application and then reopen it.
npm run dev command after you've restarted your shell.Use npx to solve the error "create-next-app: command not found", e.g.
npx create-next-app@latest or install the package globally by running
npm install -g create-next-app@latest to be able to use the command without
the npx prefix.

The fastest way to solve the error is to use the npx command.
# ๐๏ธ create js app npx create-next-app@latest # ๐๏ธ for a TypeScript project npx create-next-app@latest --typescript # ๐๏ธ Print package version npx create-next-app@latest --version
Alternatively, you can install create-next-app globally.
# ๐๏ธ install the package globally npm install -g create-next-app@latest # ๐๏ธ create js app create-next-app # ๐๏ธ for a TypeScript project create-next-app --typescript # ๐๏ธ print package version create-next-app --version
If the global installation of create-next-app fails, you might have to run the
command prefixed with
sudo.
# ๐๏ธ if you got a permissions error sudo npm install -g create-next-app@latest # ๐๏ธ create js app create-next-app # ๐๏ธ for a TypeScript project create-next-app --typescript # ๐๏ธ print package version create-next-app --version
Refer to the official npm page
of the create-next-app package for command options.
If that doesn't help, run the following command:
npm config get prefix
The command will show you the path where npm puts your globally installed
packages. The global packages will be in the bin directory at the specified
path.
Look at the PATH environment variable on your operating system and add the
path that the npm config get prefix command outputs if it's not already
there.
If that didn't work, try to add the path to the bin folder (from
npm config get prefix) to your PATH environment variable and restart your
terminal.
For example, on macOS, you can update your path with the following command:
# make sure the path matches with `npm config get prefix` output export PATH=/usr/local/share/npm/bin:$PATH
If you are on Linux, you can add the output from the npm config get prefix
command to your .bashrc file.
# ๐๏ธ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
If that doesn't help try to reinstall Node.js on your machine and then install
create-next-app globally by running npm install -g create-next-app@latest.
# ๐๏ธ install package globally npm install -g create-next-app@latest # ๐๏ธ create js app create-next-app # ๐๏ธ for a TypeScript project create-next-app --typescript # ๐๏ธ print package version create-next-app --version
create-next-app fails, you might have to run the command prefixed with sudo.# ๐๏ธ if you got a permissions error sudo npm install -g create-next-app@latest # ๐๏ธ create js app create-next-app # ๐๏ธ for a TypeScript project create-next-app --typescript # ๐๏ธ print package version create-next-app --version
Alternatively, you can see how you can fix the permissions error on this page in the official npm docs.
You can learn more about the related topics by checking out the following tutorials: