$ npm run dev
Error: Cannot find module 'next'The 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 a few hundred 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 onto your machine 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/ ~300MB of downloaded packages. 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 that was installed. It is committed to git so a second machine gets identical versions.node_modules/is not committed, because it is rebuildable from those two files. npm installdownloads everything.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. 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 repo 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 it fails, read the first error rather than the last. Everything below the first one is usually a consequence.
- Work through the five that catch everyone:
Cannot find module 'x'. You have not installed, or you are in the wrong folder. Runlsand check you can seepackage.json.Port 3000 is already in use. An older dev server is still running.npx kill-port 3000, or start on another port withnpm run dev -- -p 3001.Unsupported engineor a syntax error insidenode_modules. Your Node version is too old.node -v, then install the version the project'senginesfield 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 errors that survive a fix. A stale install or cache. Stop the
server, delete
node_modulesand.next, 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. A server has none of those.
This is the real content of "it works on my machine", and it is why Chapter 5 spends a whole section
on environments.
The dev server also lies in a useful direction. It rebuilds constantly, serves unminified code, and
shows friendly errors, 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.