Next.js Installation
Before using Next.js, you need to set up your development environment and create a new Next.js project. This chapter will guide you through these preparations.
Environment Requirements
Make sure you have the following software installed on your computer:
- Node.js: Next.js requires Node.js 12.22.0 or higher. You can download and install it from the Node.js official website.
- npm or yarn: Node.js comes with npm (Node Package Manager), and you can also choose to use yarn as your package manager.
You can check the versions of Node.js and npm with the following commands:
node -v
npm -vCreating a Next.js Project
The easiest way to create a Next.js project is to use the official scaffolding tool create-next-app.
Open your terminal and execute the following command:
npx create-next-app@latest my-next-appnpxis a tool that comes with npm 5.2+ and can execute npm packages without first installing them globally.create-next-app@latestwill use the latest version of the scaffolding tool.my-next-appis your project name, which you can replace with your preferred name.
During the installation process, the scaffolding will ask you some configuration options, such as:
- Would you like to use TypeScript with this project? (Whether to use TypeScript?)
- Would you like to use ESLint with this project? (Whether to use ESLint?)
- Would you like to use Tailwind CSS with this project? (Whether to use Tailwind CSS?)
- Would you like to use the
src/directory with this project? (Whether to use thesrc/directory?) - Would you like to use the App Router (recommended)? (Whether to use App Router?)
- Would you like to customize the default import alias? (Whether to customize the default import alias?)
You can choose Yes or No based on your needs. For beginners, it's recommended to select the default options.
Starting the Development Server
After the project is created, enter the project directory and start the development server:
cd my-next-app
npm run devIf everything goes well, you will see output similar to the following in the terminal:
- ready started server on 0.0.0.0:3000, url: http://localhost:3000
- event compiled client and server successfully in 2.3s (235 modules)Now, open http://localhost:3000 in your browser, and you will see the Next.js application welcome page.
Manual Installation
Besides using create-next-app, you can also manually install Next.js.
Initialize the Project
bashmkdir my-next-app cd my-next-app npm init -yInstall Dependencies
bashnpm install next react react-domConfigure
package.jsonOpen the
package.jsonfile and add the followingscripts:json{ "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" } }Create the
pagesDirectoryNext.js uses the
pagesdirectory to define routes. Create anindex.jsfile in this directory:bashmkdir pagesAdd the following code to
pages/index.js:jsxfunction HomePage() { return <div>Welcome to Next.js!</div>; } export default HomePage;Start the Development Server
bashnpm run dev
Now, you have successfully set up a manual Next.js project.
Summary
This chapter covered how to install and create a Next.js project. We recommend using create-next-app because it can quickly configure a modern Next.js application. In the next chapter, we will detail the Next.js project structure.
Previous Chapter: Next.js Introduction | Next Chapter: Next.js Project Structure