Thuta Learning
Next.js
BasicWeb DevelopmentIntermediate

Starting a Project and the Development Server

Next.jsLesson 320 min

What you'll walk away with

  • Check your Node.js version against the minimum in the docs before installing
  • Create a project with create-next-app, both with --yes and through the prompts
  • Explain how the dev, build and start scripts differ
  • Start the dev server, open it in the browser and stop it with Ctrl+C

Explain it simply

Explain it simply

Hitting an error during setup is a discouraging way to start a new framework. Starting a Next.js project is only four steps: check your Node.js version, create the project with create-next-app, move into its folder, and run the development server.

When you understand what each command does, you can tell at once which step is failing if something goes wrong. The dev server updates the browser automatically every time you save a file, so you see each change right away.

Problem → Why → Solution

The problem: installing with an outdated Node.js

It is common to have a Node.js that was installed years ago. Recent Next.js versions require a minimum Node.js version, and below it create-next-app or next dev stops with a message that your Node.js is too old. Worse is reinstalling packages over and over without checking the version or reading that message.

terminal
node --version
# v18.17.0   <- older than the minimum the current docs list

pnpm create next-app@latest myanmar-notes --yes
# setup or the dev server stops with a "Node.js version" message

rm -rf node_modules && pnpm install   # does not help: Node itself is too old

Why does this happen?

Next.js tooling relies on newer Node.js features and declares which versions it supports in the docs. Reinstalling packages never changes the runtime. Running node --version first and comparing it with the minimum on the installation page saves a lot of time.

Core concept: create-next-app and the three scripts

The current Next.js installation docs list Node.js 20.9 as the minimum. Older versions such as Next.js 15 also accepted Node.js 18.18, so always check the docs for the version you are installing. Using a Node.js LTS (long-term support) release is the simplest choice.

create-next-app is the official scaffolding tool. Run without --yes, it asks you one question at a time about TypeScript, ESLint, Tailwind CSS, the App Router, a src/ folder and an import alias. With --yes it creates the project straight away using the recommended defaults; at the time of writing those include TypeScript, ESLint, Tailwind CSS, the App Router and the @/* alias, and the list can change between versions. Without pnpm, use npx create-next-app@latest, yarn create next-app or bun create next-app.

ScriptWhat it does
pnpm devStarts the development server, updates the browser on every save, shows an error overlay
pnpm buildCreates the production build in .next, checks types and pre-renders pages
pnpm startServes the already-built app in production mode

The dev server compiles code on demand and uses Fast Refresh to update components while keeping their state where it can. In production you build once and serve the result with start, so how fast or slow dev mode feels says little about production performance.

When create-next-app finishes, the project folder contains app/ (routes and UI), public/ (static files), package.json, next.config.ts, tsconfig.json and the installed node_modules. The page.tsx inside app is the starter page; edit it and the browser changes almost immediately. The next lesson looks at what each folder is responsible for in detail.

Mental model: the setup chain
  1. Check the runtime with node --version
  2. Scaffold with create-next-app
  3. cd into the project
  4. Start the server with pnpm dev

If a step fails, read its error message before moving on to the next step.

General pattern

bash
node --version                          # must meet the docs' minimum
<pm> create next-app@latest <app-name>  # add --yes to accept defaults
cd <app-name>                           # every script runs from here
<pm> run dev                            # dev server, Ctrl+C to stop
<pm> run build && <pm> run start        # production build, then serve

Replace <pm> with your package manager (npm, pnpm, yarn or bun); pnpm and yarn let you drop run and type pnpm dev. Every script must be run from the project folder that contains package.json.

Example: creating the Myanmar Notes project

Open a terminal and go to the folder where you keep your projects. Then run the four commands below one at a time, reading the output after each.

bash
node --version
pnpm create next-app@latest myanmar-notes --yes
cd myanmar-notes
pnpm dev

Code breakdown

  1. Line 1node --version

    Prints a version such as v22.11.0. "command not found" means Node.js is not installed yet; install the LTS release from nodejs.org.

  2. Line 2pnpm create next-app@latest myanmar-notes --yes

    @latest uses the newest create-next-app. It creates a new myanmar-notes folder and installs the dependencies; --yes skips the questions.

  3. Line 3cd myanmar-notes

    Moves into the folder that holds package.json. Skip this and the next command cannot find the script.

  4. Line 4pnpm dev

    Runs the dev script from package.json (next dev). When Local: http://localhost:3000 appears, open it in the browser; press Ctrl+C to stop.

You should see
The terminal shows Local: http://localhost:3000, and you can see the starter page in your browser.

The scripts in package.json and the dev server output

pnpm dev simply runs dev from the scripts in package.json. Depending on the template version you may see extra flags such as --turbopack, but the shape is as below. To change the port, use next dev -p 4000.

package.json (excerpt)
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  }
}

When you run pnpm dev, the terminal prints the Next.js version, a Local: URL and a Network: URL (for opening the app from a phone on the same Wi-Fi), followed by a "Ready" line. The first visit to a page may take a moment because it is compiled on demand. When the code has an error, details appear both in the browser's error overlay and in the terminal, so check both.

Read the URL from the terminal

If another program is using port 3000, recent Next.js versions pick a different port and print it on the Local: line. Open the URL the terminal shows instead of assuming localhost:3000.

In real projects

Setup in real projects

In team projects it matters that every developer uses the same Node.js version, so teams often record it in an .nvmrc or .node-version file, or declare it in the engines field of package.json. CI servers and hosting platforms can read that version and use the same one, so "it works on my machine" problems become much rarer.

Stick to one package manager: having both pnpm-lock.yaml and package-lock.json in a repo lets dependency versions drift apart. Running pnpm build and pnpm start locally before deploying catches errors that would otherwise appear only in production. Keep the .next and node_modules folders out of git; the .gitignore from create-next-app already excludes them, and anyone who clones the repo restores them with pnpm install and pnpm build.

Common mistakes

Common mistakes

1. Installing without checking the Node.js version

Reinstalling packages does not fix a version error. Run node --version, and if it is below the minimum in the docs, update Node.js; a version manager such as nvm lets you switch versions per project.

2. Running pnpm dev outside the project folder

After create-next-app finishes, the terminal is still in the parent folder. Running pnpm dev there fails because there is no package.json. Check where you are with pwd (cd on Windows) and run cd myanmar-notes.

3. Running pnpm start without building first

next start serves the production build in the .next folder. Without one, it stops with an error saying no production build was found. Run pnpm build first, and rebuild after every code change.

Best practices

Best practices

  • Create your first project without --yes so you learn what each prompt asks; use --yes once you know the answers.
  • Keep the dev server in one terminal tab and use another tab for git and other commands.
  • Do not leave several servers running; stop the ones you no longer need with Ctrl+C, or ports get confusing.
  • Run pnpm build after each feature to catch type errors and build errors early.

Mini challenge

Mini challenge: a Node.js version preflight script

Write a TypeScript script to run before setup. It compares version strings with the minimum "20.9.0" and prints OK or too old.

  • Turn a string like "v20.10.1" into the number array [20, 10, 1], dropping the leading v
  • isAtLeast(version, minimum) compares major, minor and patch in order, as numbers
  • Print the result for v18.19.0, v20.9.0, v20.10.1 and v22.11.0
  • Finally, check process.version (this machine's Node.js) too
check-node.ts
const MINIMUM = "20.9.0";

function parseVersion(version: string): number[] {
  // TODO: drop a leading "v", split on ".", convert to numbers
}

function isAtLeast(version: string, minimum: string): boolean {
  // TODO: compare major, then minor, then patch
}

function report(version: string): string {
  // TODO: "v20.9.0: OK" or "v18.19.0: too old, install 20.9.0 or newer"
}

// TODO: report the four sample versions, then process.version

Expected: v18.19.0: too old, install 20.9.0 or newer v20.9.0: OK v20.10.1: OK v22.11.0: OK The last line depends on your machine, for example This machine -> v22.20.0: OK

Show hint

Comparing "20.10.1" >= "20.9.0" as strings checks character by character, so "1" < "9" makes it false. Convert each part to a Number and decide at the first part that differs.

Show solution
check-node.ts
const MINIMUM = "20.9.0";

function parseVersion(version: string): number[] {
  return version.replace(/^v/, "").split(".").map(Number);
}

function isAtLeast(version: string, minimum: string): boolean {
  const have = parseVersion(version);
  const need = parseVersion(minimum);
  for (let i = 0; i < 3; i++) {
    const a = have[i] ?? 0;
    const b = need[i] ?? 0;
    if (a !== b) return a > b;
  }
  return true;
}

function report(version: string): string {
  return isAtLeast(version, MINIMUM)
    ? `${version}: OK`
    : `${version}: too old, install ${MINIMUM} or newer`;
}

for (const sample of ["v18.19.0", "v20.9.0", "v20.10.1", "v22.11.0"]) {
  console.log(report(sample));
}
console.log("This machine ->", report(process.version));

Quick quiz 1 / 3

create-next-app has finished and the terminal is still in the parent folder. What happens if you run pnpm dev?

Quick quiz 2 / 3

In JavaScript, what does "20.10.1" >= "20.9.0" (two strings) evaluate to?

Quick quiz 3 / 3

You want to try the production version locally before deploying. What do you run?

What to remember

  • Before installing, run node --version and compare it with the minimum in the docs for your Next.js version.
  • create-next-app --yes creates a project with the recommended defaults; without --yes it asks about each option.
  • dev is for development, build creates the production build, and start serves that build.
  • Run scripts from the folder with package.json, and read the URL from the terminal's Local: line.

Next.js — Installation — Next.js

Next.js — Installation — Next.js

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.