Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Next.js Installation

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? @/*

After running the command, create-next-app automatically performs the following operations:

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:

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

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.