Next.js Installation
Before you begin, make sure Node.js and npm are installed. You can check whether they are installed using the following command:
node -v
npm -v
If your system does not yet support Node.js and NPM, you can refer to our Node.js Tutorial.
In China, npm can be slow; you can use Taobao's customized cnpm (with gzip compression support) command-line tool to replace the default npm:
$ npm install -g cnpm --registry=https://registry.npmmirror.com
$ npm config set registry https://registry.npmmirror.com
You can then use the cnpm command to install modules:
$ cnpm install [name]
For more information, see:http://npm.taobao.org/.
Quickly build a Next.js development environment with create-react-app
Next.js provides an official scaffolding tool, create-next-app, for quickly setting up a project.
create-react-app comes from Facebook; with this command we can quickly build a Next.js development environment without any configuration.
The project automatically created by create-react-app is based on Webpack + ES6.
Run the following command to create the project:
npx create-next-app@latest my-next-app
During installation, you will see the following prompts; just press Enter all the way through:
Would you like to use TypeScript? No / Yes
Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like your code inside a `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to use Turbopack for `next dev`? No / Yes
Would you like to customize the import alias (`@/*` by default)? No / Yes
What import alias would you like configured? @/*
- Use
create-next-appThe scaffolding tool creates a new Next.js project. my-next-appis the name of the project; you can change it to any name as needed.- It automatically installs the project's required dependencies.
After running the command, create-next-app automatically performs the following operations:
- Create a folder named my-next-app.
- Initialize the project and install the necessary dependencies.
- Create the basic project structure, including the app folder, public folder, package.json file, and so on.

After creation is complete, enter the project directory:
cd my-next-app
In the project directory, run the following command to start the development server:
npm run dev
Or, if you use yarn:
yarn dev
The development server runs by default at http://localhost:3000, open your browser and visit **http://localhost:3000**; you should see Next.js's default welcome page.

We can modify the page.tsx file in the app directory; the modified code is:
app/page.tsx
export
default
function
Home
(
)
{
return
<
div
Hello
,
World!
div
;
}
Visit **http://localhost:3000**; the display is as follows:
Hello, World!
Manually Install Next.js
To manually create a new Next.js application, first install the required packages:
npm install next@latest react@latest react-dom@latest
Next, open your package.json file and add the following scripts:
package.json file:
{
"scripts"
:
{
"dev"
:
"next dev"
,
// Start Next.js in development mode
"build"
:
"next build"
,
// Build the application for production
"start"
:
"next start"
,
// Start the Next.js production server
"lint"
:
"next lint"
// Configure Next.js's built-in ESLint
}
}
These scripts represent the different stages of developing an application:
- dev: runs
next dev, to start Next.js in development mode. - build: runs
next build, to build the application for production. - start: runs
next start, to start the Next.js production server. - lint: runs
next lint, to set up Next.js's built-in ESLint configuration.
Create the Application Directory
Next.js uses file-system routing, which means the application's routes are determined by the file structure.
Create an app folder, then add a layout.tsx and a page.tsx file. When a user visits the application root path /, these files are rendered.
Create the Root Layout File
in app/layout.tsx Create the root layout file in , containing the required and tags:
Example
export
default
function
RootLayout
(
{
children
,
}
:
{
children
:
React.
ReactNode
}
)
{
return
(
<
html lang
=
"en"
<body{children}body html)}>Finally, create a home page file in app/page.tsx and add the initial content:
Example
export
default
function
Page
(
)
{
return
<
h1
Hello
,Next.
js
!
h1
}
Notes
- If you forget to create
layout.tsx, Next.js will, when running the development server (next dev), automatically create this file. - You can optionally use
srcdirectory to separate application code from configuration files.
Create the public Folder (Optional)
You can optionally create public folder to store static assets such as images, fonts, and so on.public Files in the folder can be accessed via / paths.
This way, you complete the manual installation and initialization of a basic Next.js project structure.
Run the Development Server
Run the following command to start the dev server:
npm run dev
Visit http://localhost:3000 to view the application.
Edit the app/page.tsx file and save; you can then see the updated result in the browser.