The Wayback Machine - https://web.archive.org/web/20240919123219/https://www.geeksforgeeks.org/next-js-installation/
Open In App

Next.js Installation

Last Updated : 30 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Next.js is a popular React framework that enables server-side rendering and static site generation. It is easy to learn if you have prior knowledge of HTML, CSS, JavaScript, and ReactJS. Installing Next.js involves setting up Node.js and npm, creating a new Next.js project using npx create-next-app, and starting the development server with npm run dev.

Note: This installation is for automatic setup in which folders and files are created automatically.

Steps to Install Next.js

Step 1: Install Node in Your System

Install NodeJS. Follow one of the links to install according to your system, Windows, Linux and MacOS.

Step 2: Initialize the Next.js Project

Now create a folder for your project on the desktop navigate to the folder through your code editor and run the following command on the terminal.

npx create-next-app@latest 
#OR
yarn create next-app
#OR
pnpm create next-app

Step 3: Configure your next.js app

Write the project name (default my-app) and choose other configuration options

√ What is your project named? my-app
√ 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 to use `src/` directory? No / Yes
√ Would you like to use App Router? (recommended) No / Yes
√ Would you like to customize the default import alias (@/*)? No / Yes
√ What import alias would you like configured? @/*

Step 4: Switch to Project Directory

Move to the project folder to install any other other dependencies. Use the command given below

cd my-app

Project Structure:

This would be the folder structure after selecting No for App Router while installation

Next.js Setup

Next.js Project Structure

Step 5: Run the Application

Write the command below to run the NextJS Application, and then open the URL in the browser.

npm run dev

After the successful run, the default page of next.js will be shown in the browser.

Let’s understand working through an example.

Example 1: Hello World in NextJS

In this example, we will simply print Hello. The index.js file is the main entry point of the next.js application, which means the component exported default by the index.js file gets rendered on the screen, so we have to change inside this file to render hello world on the screen.

JavaScript
// pages/index.js

export default function Home() {
    return (
        <div>Hello, GeeksforGeeks Learner!!</div>
    );
}

Output:

Example 2: Routing in Next.js

Example based on Pages. NextJS provides an awesome feature of pages, which means you can create any component same as the ReactJS, and then store it inside a directory named pages. Later whenever anyone visits the route /component, the component will render. You can clearly see what’s going on here, we are creating routing without any extra effort.

Project Structure:

We are going to create three components here, this will be the folder structure.

Define the Pages:

Inside the functional component, there is a div in starting which contains some text and bold text inside the b tag.

JavaScript
// pages/page1.js

function Page1() {
    return (
        <div>
            This is page1, accessed from the route
            <b>/page1</b>
        </div>
    );
}

export default Page1;
JavaScript
// pages/page2.js

function Page2() {
    return (
        <div>
            This is page2, accessed from the route
            <b>/page2</b>
        </div>
    );
}

export default Page2;

Define Routes:

Create this file inside the directory dynamicpage (let say) and the component will render on the route /dynamicpage/[id] where [] denotes the place holder. The router object returned from the useRouter hook provides an attribute asPath which contains the path of the requested URL that we are accessing inside the b tag.

JavaScript
// pages/dynamicpage/[id].js

import { useRouter } from "next/router";

function DynamicPage() {
    const router = useRouter();
    return (
        <div>
            This is Dynamic Page, accessed from
            the route <b>{router.asPath}</b>{" "}
        </div>
    );
}

export default DynamicPage;

Uodate the Home Page:

Finally, here we have combined all three pages inside the entry point of next.js app

JavaScript
// pages/index.js

export default function Home() {
    return <div>This is HomePage
        <p>
        Click on this <a href='/page1'><u>Link</u></a> to Go
        <b>/page1</b>
        Route
        </p>
    
        <p>
            Click on this <a href='/page2'><u>Link</u></a> to Go
            <b>/page2</b> Route
        </p>
    
    </div>
    }
    

Output:



Previous Article
Next Article

Similar Reads

How to pass variables to the next middleware using next() in Express.js ?
The following example covers how to pass variables to the next middleware using next() in Express.js. Approach: We cannot directly pass data to the next middleware, but we can send data through the request object. [Middleware 1] [Middleware 2] request.mydata = someData; -------&gt; let dataFromMiddleware1 = request.mydata; Installation of Express m
2 min read
Next.js next/head
In this article, we'll learn about the Head component in NextJS and see how the head component is SEO friendly. The Head component is a built-in component provided by NextJS. We can add any element like we were adding in the head component of HTML. If you know HTML and CSS well then NextJS will be easy for you to learn. To keep everything simple th
4 min read
When to use next() and return next() in Node.js ?
In Node.js, particularly when working with the Express framework, middleware functions play a crucial role in handling requests. Two commonly used patterns within these middleware functions are next() and return next(). Understanding when and how to use these can significantly affect the flow and functionality of your application. Table of Content
4 min read
Next.js Create Next App
In Next.js, the create next app command is used to automatically initialize a new NextJS project with the default configuration, providing a streamlined way to build applications efficiently and quickly. System Requirements: Node.js 12.22.0 or laterNPM 6.14.4 or later OR Yarn 1.22.10 or latermacOS, Windows, and Linux are supportedGetting started wi
3 min read
Next.js next/link
In Next.js, next/link component enables client-side navigation between pages, enhancing performance by prefetching linked pages. It simplifies routing in your application, making it seamless and efficient. Table of Content Next.js next/link ComponentBasic usage of 'next/link'Dynamic routing with 'next/link'ConclusionNext.js next/link ComponentThe n
4 min read
Next.js next/amp
AMP (Accelerated Mobile Pages) is a web component framework developed by Google that enables the creation of fast-loading web pages. Next.js supports AMP out of the box, allowing you to create AMP pages seamlessly. What is Next.js AMP?AMP is designed to improve the performance of web pages, particularly on mobile devices. By using a subset of HTML
3 min read
Next.js next.config.js File
The next.config.js file in a Next.js application is a configuration file used to customize and extend the behavior of Next.js. It allows developers to adjust various settings such as routing, environment variables, image optimization, and more to tailor the application to their specific needs. What is next.config.js?The next.config.js is a configur
3 min read
LAMP installation and important PHP configurations on Ubuntu
PHP is the Web development (Server Side Scripting Language) written and developed for web developers.It was developed by Rasmus Lerdorf in 1994, for maintaining his personal homepage. Later he added the feature to communicate with web forms and to connect with databases, named it as Personal Home Page Tools (PHP), Lerdorf wrote those using C binari
5 min read
Angular 7 | Installation
To install Angular 7 on your machine you have to require the following things to be installed in your machine. Install Visual Studio Code IDE or JetBrains WebStorm.Install Node.jsUsing npm to install angular cli Follow the steps to set up an Angular 7 Environment: Step 1: Install Visual Studio Code IDE (or JetBrains WebStorm) Visual Studio Code is
2 min read
Laravel | Installation and Configuration
Laravel is a PHP framework that makes building complex web applications a cakewalk. In this guide you are going to learn how to Install and Configure Laravel and get your first Laravel project running in your computer. Prerequisites: Basic knowledge of PHPPHP InstallationBasic knowledge of Terminal/Command Prompt Steps to install Laravel: Laravel a
2 min read
p5.js | SoundFile Object Installation and Methods
The SoundFile object is a part of the p5.sound which is a library of JavaScript. This object contains so many functions nearly 30+ functions are there. Each one of them is useful to design a sounded website. By using this object you can easily add audio into your website and play, pause, stop anything you want. To do so you just need to attach the
4 min read
Svelte | Introduction and Installation
Svelte is the new methodology for creating web apps. It can be used in a small part of a code or in an entire single page application. It is a compiler not a framework, which is faster than other JavaScript libraries like ReactJS, AngularJS, VueJS. It is used to create reactive web apps. If any change occurs in the data that change will reflect on
2 min read
CakePHP Framework | Installation and Configuration
CakePHP is an open-source framework for PHP based on a Model–View–Controller (MVC)-like architecture and modeled after the concepts of Ruby on Rails, which is powerful as well as easy to learn. It's a foundational structure for programmers to create web applications. Models, Views, and Controllers are used for the separation of business logic from
1 min read
Introduction and Installation process of Gulp
Gulp can be defined as a task runner or a toolkit for automating time-consuming tasks such as magnification, concatenation, cache busting, unit testing and linting in web development. It uses Node.js as a platform and utilizes JavaScript code to help in running front-end tasks and large-scale web applications. These tasks can be run on PowerShell,
3 min read
PugJS Basics and Installation
Pug.js is a template engine for Node.js and browsers to render dynamic reusable content. At compile time, the template engine compiles our Pug template code to HTML. We can use variables in pug just like in JavaScript. At runtime, the template engine injects or replaces variables in a template file with actual values, and transforms the template in
3 min read
Ant Design Introduction and Installation for Angular
Ant Design is a design pattern for enterprise-level products that can be integrated with other front-end frameworks such as Angular, React, or Vue. Ant Design official implementation is released with React but it can be used with other JavaScript frameworks. It is an open-source tool with approximately 50.4K GitHub stars, it is the world's second m
3 min read
MDBootstrap Introduction and Installation for Angular
Introduction: MDBootstrap (Material Design for Bootstrap) is a complete UI package that can be integrated with other frameworks such as Angular, React, Vue, etc. It is used to design a fully responsive and mobile-friendly layout using various components, plugins, animations to make it more attractive and user friendly which are compatible with othe
3 min read
Sails.js Basics and Installation
Node.js: Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and it’s not a programming language. Most of the people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end serv
2 min read
SweetAlert Library Introduction and Installation with the Example
When building JavaScript-driven websites we often have the need to provide feedback to our users to let them know if the action they have performed has been successful or not. In the early days of the web, developers used to create messages using the window.alert() function. While alert() works in practice and it’s also consistent across browsers,
5 min read
script.aculo.us Installation
What is script.aculo.us? script.aculo.us is a set of open-source JavaScript libraries designed to enhance the user interface of web sites. It provides visual effects engine, a drag and drop library, controls (sliders, in-place editing, Ajax-based autocompletion), and more via the Document Object Model (DOM). It was first released to the public in J
3 min read
AngularJS End to End (E2E) Testing Protractor Installation and Setup
The Protractor is an end-to-end test framework developed for Angular and AngularJS applications. It runs tests against the application interacting with it as a real user would, running in a real browser. Features of Protractor: Tests using browser: Protractor uses native events and browser-specific drivers to interact with the application.Build for
2 min read
AsgardCMS Introduction and Installation
AsgardCMS is made for developers. It is a full-featured, multilingual modular CMS developed on top of the Laravel framework. The built-in quick-development tools allow the developer to avoid spending their time on design and concentrate on functionality. It has the following characteristics: Completely Responsive: It has Bootstrap, which is fully r
3 min read
DayJS Introduction and Installation
Day js is a lightweight date and time manipulation library. It is an alternative to moment js with the same modern API. The latest version of day js has a size of 7Kb (minified) and 2Kb (Gzipped). Due to its small size, it takes less time to load and thus improving the overall performance of the application. Features: It is easy to use and since it
2 min read
ASP Introduction and Installation
ASP stands for Active Server Page. It is commonly known as Classic ASP or ASP Classic. ASP is a server-side scripting language that is used for executing the scripts on the webserver. This technology was developed by Microsoft. It is an old but still useful tool used to create dynamic webpages. ASP is a program that runs inside a web server. Active
3 min read
Introduction and Installation of Nightmare.js
Nightmare is provided by Segment and is a high-level browser automation library. The goal for this Nightmare is to follow the path of the user actions (like goto, type, click, etc.), in order to expose few simple methods with an API that feels synchronous for each block of scripting, rather than deeply nested callbacks. It has been designed for aut
2 min read
Pure CSS Installation and Uses
Pure CSS is a framework of CSS. It is a free and open-source tool collection for creating responsive websites and web applications. Pure CSS is developed by Yahoo and is used for creating faster, beautiful, and responsive websites. It can be used as an alternative to Bootstrap. Installation of Pure CSS: We can add Pure CSS to our webpage in two way
2 min read
Selenium JavaScript Installation
Selenium is a tool that helps us automate anything present on the web. Breaking this definition down to even simpler terms than selenium provides us the ability to interact with the web browser automatically using codes. From login to a website to like, share, or comment on a post we can do it all automatically with the help of selenium. So if you
3 min read
What is local installation ?
Local installation involves the downloading of static files like CSS locally on our system and then using them on our web page. This ensures that the given copy of CSS is loaded from our local machine. This is different than using CSS files hosted on a CDN where the files will be loaded by making a call to the CDN. There are numerous use cases wher
2 min read
Ember.js Installation
Basically, Ember.js is an open-source JavaScript framework developed by Yehuda Katz for creative and ambitious web developers. It is a Java-Script framework to developed modern web applications, it combines everything that you need to build fancy UIs that work on any device. Ember.js has a very useful data management system to help organize your da
2 min read
How local installation of a dependency done ?
The local installation of dependencies in Node.js is done by putting the local dependencies in a single place and running some simple commands from the terminal to install dependencies. A local dependency can't be accessed outside of the directory. All the local dependencies of a project are usually inside of a file called package.json Prerequisite
2 min read