๐ 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
๐ป System Requirements
Before installing anything, make sure your machine meets the minimum requirements for running a modern Next.js application.
| Requirement | Minimum Version | Notes |
|---|---|---|
| Node.js | 18.18.0+ | LTS versions strongly recommended |
| macOS | 10.13+ | Fully supported |
| Windows | 10+ | WSL2 recommended for best performance |
| Linux | Any modern distro | Fully supported |
Warning
โ๏ธ 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.
- Download the installer directly from the official Node.js website.
- Use a version manager like nvm (Node Version Manager) to install and switch between versions.
- Use your operating system's package manager (e.g. brew, apt, choco).
Verify installation
node -v
npm -vTip
๐ฆ 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 reactYarn
Yarn was created to improve on early npm limitations, offering faster installs and a reliable lockfile system.
Installing a package with Yarn
yarn add reactpnpm
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 reactBest Practice
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 reactCreate Next App with npm
npx create-next-app@latestCreate Next App with yarn
yarn create next-appCreate Next App with pnpm
pnpm create next-appCreate 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-appYou'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-domThen add the following scripts to your package.json:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}Caution
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
๐๏ธ Project Structure
Understanding the generated folder structure helps you navigate and extend your project confidently.
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
๐ง 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/nodeESLint 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 lintTailwind 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
Important
๐ฆ 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 devBy 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 buildInformation
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
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
- Always use an LTS version of Node.js for stability.
- Commit your lockfile (package-lock.json, yarn.lock, or pnpm-lock.yaml) to version control.
- Keep secrets out of source control using .env.local.
- Prefer the App Router for new projects.
- Regularly update dependencies to receive security patches.
Summary
๐ฏ 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! ๐