$ npm run dev
sh: next: command not found
npm error code 127The code is fine. Nothing is installed yet. Most of the failures in your first week are this category: the project is asking for something your machine does not have, in a folder it is not looking in.
The idea
The code you and your assistant write is a small fraction of what runs. The rest arrives as Packages of code written by other people that your project uses instead of writing its own. Your machine downloads them; you do not write them., downloaded by a The program that reads your project's dependency list, downloads each package, and records the exact versions it used. such as npm or pnpm. Running the project means installing those, then starting a dev server that compiles your files and serves them to your browser at an address on your own computer.
package.json "next": "16.0.1", "react": "19.0.0"
|
| npm install (reads the list, writes the lockfile)
v
node_modules/ the downloaded packages. Large, and never committed.
|
| npm run dev
v
dev server (a running program, holds your terminal)
|
v
http://localhost:3000 localhost = this machine. 3000 = the port.How it works
package.jsonlists what the project needs and defines the shortcut commands underscripts, sonpm run devruns whatever that project decideddevmeans.- The lockfile (
package-lock.jsonfor npm,pnpm-lock.yamlfor pnpm) records the exact version of every package installed. It is committed to git so a second machine gets identical versions.node_modules/is not committed, because it can be rebuilt from those two files. npm installdownloads everything the list asks for.npm install resendadds one new package and updates both files.- A A program that rebuilds your project as you edit and serves it to your browser, with error messages meant for you rather than for a visitor. keeps running until you stop it with Control+C, and the terminal tab it occupies is busy for as long as it runs.
localhostmeans this computer, and the A numbered channel on your machine. Two programs cannot listen on the same one at the same time. after the colon says which program to reach. Next.js defaults to 3000, Vite to 5173.
What to do
- Install once per project, in the project root:
npm install, orpnpm installif the repository has apnpm-lock.yaml. Mixing the two in one project produces two lockfiles and hours of confusion. - Start it with
npm run devand open the address it prints. Leave that tab alone. - When a build fails, fix the first error and run it again. The errors below the first one are often consequences of it, and they disappear on their own.
- Work through the five that catch everyone:
command not found, orCannot find module 'x'. Nothing is installed, or you are in the wrong folder. Runlsand check you can seepackage.json.Port 3000 is in use, trying 3001 instead. An older dev server is still running. Next.js usually just moves to the next free port, so read the address it prints rather than typing 3000 from memory. To reclaim 3000, press Control+C in the old tab, or runnpx kill-port 3000. (npxruns a package once without installing it into your project.)- A crash inside
node_modules, or npm warning about an unsupported engine. Usually your Node version. Runnode -vand compare it against what the project's README orenginesfield asks for. undefinedwhere a key should be. A missing A setting supplied to the program when it starts, kept outside the code because it usually holds a secret.. Copy.env.exampleto.env.localand fill it in.- Changes that do not appear, or an error that survives its own fix. A stale install or cache.
Stop the server, delete
node_modulesand the framework's build folder (.nextfor Next.js), and install again.
Where it breaks
Working on your machine proves less than it feels like it proves. Your laptop has your Node version,
your .env.local, your cached build and a case-insensitive filesystem, and a server has none of
those. This is the real content of "it works on my machine", and it is why section 6 gives
environments a section of their own.
The dev server also flatters you. It rebuilds constantly, keeps the code readable instead of
compressing it, and shows errors written for you rather than for a stranger, so it is faster and
more forgiving than the production build. npm run build is the command
that tells you the truth, and it is worth running before you promise anyone anything.