Installation & Project Setup

๐Ÿ“– Introduction

Welcome to this comprehensive guide on installing and setting up a Next.js project from scratch! Whether you're a beginner starting your React journey or an experienced developer exploring Next.js for the first time, this tutorial will walk you through every step needed to get a production-ready environment running.

By the end of this tutorial, you'll understand how to install Node.js, choose the right package manager, scaffold a new project, understand the resulting folder structure, configure essential tools, and run your app in both development and production modes. ๐Ÿš€

Information

This tutorial assumes basic familiarity with CLI tools and JavaScript fundamentals.

๐Ÿ’ป System Requirements

Before installing anything, make sure your machine meets the minimum requirements for running a modern Next.js application.

RequirementMinimum VersionNotes
Node.js18.18.0+LTS versions strongly recommended
macOS10.13+Fully supported
Windows10+WSL2 recommended for best performance
LinuxAny modern distroFully supported

Warning

Using an outdated Node.js version is one of the most common causes of installation failures. Always verify your version before proceeding.

โš™๏ธ Installing Node.js

Node.js is the JavaScript runtime that powers the entire tooling ecosystem around Next.js. There are several ways to install it.

  1. Download the installer directly from the official Node.js website.
  2. Use a version manager like nvm (Node Version Manager) to install and switch between versions.
  3. Use your operating system's package manager (e.g. brew, apt, choco).

Verify installation

node -v
npm -v

Tip

Prefer using nvm so you can easily switch Node.js versions across different projects without conflicts.

๐Ÿ“ฆ Understanding Package Managers

A package manager handles installing, updating, and managing the JavaScript dependencies your project relies on. Next.js is compatible with several popular options, each with its own trade-offs in speed, disk usage, and features.

npm

npm is the default package manager bundled with Node.js. It's the most widely used and has the largest ecosystem support.

Installing a package with npm

npm install react

Yarn

Yarn was created to improve on early npm limitations, offering faster installs and a reliable lockfile system.

Installing a package with Yarn

yarn add react

pnpm

pnpm uses a content-addressable storage system, meaning packages are stored once on disk and linked across projects โ€” saving significant disk space.

Installing a package with pnpm

pnpm add react

Best Practice

For large monorepos or multiple projects, pnpm is often the fastest and most disk-efficient choice. ๐Ÿ’พ

Bun

Bun is an all-in-one JavaScript runtime and package manager known for its exceptional speed, thanks to being built on a low-level engine rather than JavaScript itself.

Installing a package with Bun

bun add react

Create Next App with npm

npx create-next-app@latest

Create Next App with yarn

yarn create next-app

Create Next App with pnpm

pnpm create next-app

Create Next App with bun

bun create next-app

๐Ÿ—๏ธ Creating a Next.js Project

Now that Node.js and a package manager are ready, it's time to scaffold your first Next.js project.

Using create-next-app

The official and recommended way to start a new Next.js project is via the create-next-app CLI tool, which sets up everything automatically.

Scaffold a new project

npx create-next-app@latest my-app

You'll be prompted with several configuration questions:

  • Would you like to use TypeScript?
  • Would you like to use ESLint?
  • Would you like to use Tailwind CSS?
  • Would you like to use the src/ directory?
  • Would you like to use the App Router?

Manual Installation

For full control, you can set up a Next.js project manually without the CLI scaffolding tool.

Manual setup

mkdir my-app && cd my-app
npm init -y
npm install next react react-dom

Then add the following scripts to your package.json:

package.json

{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  }
}

Caution

Manual installation requires you to configure TypeScript, ESLint, and folder structure yourself โ€” it is not recommended for beginners.

Choosing the App Router

The App Router, introduced in Next.js 13, is built on React Server Components and is the modern, recommended approach going forward.

  • Supports layouts, nested routing, and streaming out of the box.
  • Enables Server Components for improved performance.
  • Actively developed with new features prioritized here.

Choosing the Pages Router (Legacy)

The Pages Router is the original Next.js routing system, still supported for backward compatibility.

Note

The Pages Router is considered legacy. New projects should default to the App Router unless maintaining an existing codebase.

๐Ÿ—‚๏ธ Project Structure

Understanding the generated folder structure helps you navigate and extend your project confidently.

my-app
app
layout.tsx
page.tsx
globals.css
next.config.ts
tsconfig.json
.eslintrc.json
package.json

The app Directory

The app directory is the heart of the App Router. Each folder inside it maps directly to a route segment, and special files like page.tsx and layout.tsx define what renders at that route.

The public Directory

Static assets such as images, fonts, and icons live in the public directory. Files here are served from the root URL path.

The src Directory

Optionally, you can place your app directory (and other source code) inside a top-level src folder to keep configuration files cleanly separated from application code.

Tip

Using src/ is a matter of preference โ€” it doesn't change functionality, only organization. ๐Ÿงน

๐Ÿ”ง Configuration Files

Next.js relies on several configuration files to customize build behavior, linting, styling, and type checking.

next.config.ts

This file allows you to customize build-time and runtime behavior of your Next.js app.

next.config.ts

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  reactStrictMode: true,
};

export default nextConfig;

TypeScript Setup

If enabled during scaffolding, Next.js automatically generates a tsconfig.json and installs the necessary type definitions.

Manually adding TypeScript

npm install --save-dev typescript @types/react @types/node

ESLint Setup

ESLint helps catch bugs and enforce consistent code style. Next.js ships with a built-in ESLint configuration tailored for React and Next.js projects.

Running the linter

npm run lint

Tailwind CSS Setup

Tailwind CSS is a utility-first CSS framework that integrates seamlessly with Next.js when selected during setup.

globals.css

@tailwind base;
@tailwind components;
@tailwind utilities;

Environment Variables

Environment variables allow you to store secrets and configuration outside your codebase. Next.js automatically loads variables from .env.local.

.env.local

DATABASE_URL="postgres://user:pass@localhost:5432/db"
NEXT_PUBLIC_API_URL="https://api.example.com"

Danger

Never commit .env.local to version control โ€” it may contain sensitive credentials. Add it to .gitignore.

Important

Only variables prefixed with NEXT_PUBLIC_ are exposed to the browser; all others remain server-only.

๐Ÿšฆ Running the Development Server

Once your project is set up, start the local development server to preview your app with hot reloading enabled.

Start dev server

npm run dev

By default, your application will be available at http://localhost:3000.

๐Ÿ“ฆ Building for Production

Before deploying, you must create an optimized production build of your application.

Build the app

npm run build

Information

The build process performs static optimization, code splitting, and generates production-ready output in the .next folder.

Starting the Production Server

After building, launch the production server with the following command:

Start production server

npm run start

๐Ÿ› ๏ธ Development Workflow

A smooth development workflow relies on the right set of tools to inspect, debug, and monitor your application.

React Developer Tools

The React Developer Tools browser extension lets you inspect the component tree, props, and state directly in your browser's dev tools panel.

Next.js Developer Tools

Next.js includes built-in overlays for error reporting, build diagnostics, and performance insights during development.

Updating Next.js

Keeping Next.js up to date ensures access to the latest features, performance improvements, and security patches.

Upgrade Next.js

npm install next@latest react@latest react-dom@latest

โ— Common Installation Issues

Even with careful setup, a few recurring issues tend to trip up newcomers.

  • Node version mismatch โ€” causes cryptic build errors.
  • Permission errors during global installs on macOS/Linux.
  • Port conflicts when 3000 is already in use.
  • Stale cache issues after switching branches or dependencies.

Troubleshooting

Installation Issue
Build fails
Port already in use
Dependency errors
Check Node.js version
Clear .next cache
Kill process on port 3000
Run with a custom port
Delete node_modules
Reinstall with a clean lockfile

Common fixes

# Run on a different port
npm run dev -- -p 3001

# Clear cache and reinstall
rm -rf .next node_modules
npm install

โœ… Best Practices

  1. Always use an LTS version of Node.js for stability.
  2. Commit your lockfile (package-lock.json, yarn.lock, or pnpm-lock.yaml) to version control.
  3. Keep secrets out of source control using .env.local.
  4. Prefer the App Router for new projects.
  5. Regularly update dependencies to receive security patches.

Summary

A clean, well-organized setup from the start saves significant time during later stages of development. ๐ŸŒฑ

๐ŸŽฏ Summary

You're now fully equipped to start building with Next.js! From here, the next step is diving into routing, data fetching, and component architecture. Happy coding! ๐ŸŽ‰