ReactJS-The Beginner Master Class
  • React In the Beginning
    • Lesson 1 - Demo: Build a Simple React App - Fast
    • Lesson 2 - HTML Review
    • Lesson 3 - CSS Review
    • Lesson 4 - Modern JavaScript (JSX) Patterns
    • Lesson 5 - Set up Dev Environment
    • Hands-on Practice
  • React Fundamentals
    • Lesson 1 - Understanding Old vs New Way of Building Web Apps - SPAs
    • Lesson 2 - Motivation for Using React as the Solution to Vanilla JS
    • Lesson 3 - What is ReactJS - How it Works
    • React Code Along from Scratch
    • Lesson 4 - Create and Run a React Project with Vite - Full Overview
    • Lesson 5 - Create Hook by State Management in React
    • Lesson 6 - React Project File and Folder Walkthrough
    • Lesson 7 - JSX and How React Treats the DOM & JSX Compilation(by Babeljs) - Overview
    • Lesson 8 - Understanding the Main Files - App.jsx and main.jsx
    • Lesson 9 - Props and One-Way Data Flow - Overview
    • Lesson 10 - Google Bookshelf App - Ver 1.0
    • Hands-on Practice I
    • Hands-on Practice II
  • React State and Styling
    • Lesson 1 - Pulling Book Data from a Different Data File
    • Lesson 2 - Overview of How State Works in React
    • Lesson 3 - RandomQuote App
    • Lesson 4 - Lifting State Up - React Pattern Overview
    • Hands-On - Simple Counter
  • Forms and Interactivity - Grocery List App
    • Lesson 1 - Setup a Simple Form and Input
    • Lesson 2 - Build Form Profile App Using Multi-input Form Data
    • Hands-on : Build a Grocery List App
  • Connecting to the Backend - Consuming APIs - UseEffect Hook
    • Lesson 1 - Connecting to the Back End - Understanding Side Effects, Hooks and useEffect - Overview
    • Lesson 2 - Fetching Data from the Backend the Right Way with useEffect Hook
    • Lesson 3 - Setting Up Loading State
    • Hands-on :Use Dependency Array and Adding Values that Control Side Effects
  • Solo Project 1
  • RESTful APIs :Build a BookSearch App -Ver 2.0
    • Lesson 1: Build and test RESTful APIs with Postman
    • Lesson 2 - BookShelf App Structure
    • Lesson 3 - Create NavBar.jsx Component
    • Lesson 4 - Create Footer Component
    • Lesson 5 - Create BookList.jsx Component
    • Lesson 6 - Create BookCard.jsx Component
    • Lesson 7 - Creating Custom Hooks - useBooks and api-client
    • Lesson 8 - Controlling Network Activities in React with AbortController
    • Lesson 9 - Show Book Details in a Modal - Working
    • Lesson 10 - Bookshelf App Summary
  • Multi-Page Applications (MPAs)
    • Build a Multi-Page React Application
    • Multi-Page React Application
    • Hands-on Practice
  • Backend Frameworks-NEXT.JS
    • Migrating from React to Next.js
    • Lesson 1: Key Concepts of NodeJS and Express for Backend Web Development
    • Lesson 2: How to set up a new Next.js project
    • Lesson 3: How to create Layouts and Pages
    • Hands-on Practice 1
    • Hands on Practice 2
      • New Project & Folder Structure
      • File-Based Routing
      • Server vs Client Components & Router Hooks
      • Start On The Navbar
      • Navbar Links, Dropdowns & React Icons
      • Active Links & Conditional Rendering
      • Homepage Components
      • Properties Page
      • Property Card Dynamic Data
      • Home Property Listings
      • Custom Not Found & Loading Pages
  • Git and GitHubs
    • Git Installation
    • Git Commands
    • GitHub Repository
    • Hands-on Practice
  • Database in Application
    • React Supabase CRUD
    • Hands-on: Note Taking App
  • NoSQL Database
    • Installing MongoDB Community Edition
    • System Env Path Setting
    • How to use MongoDB Shell
    • How to Connect and Use Mongosh in VS Code via MongoDB Extension
    • MongoDB Guide
  • Solo Project 2
  • Deployment and Web Hosting
    • Lesson 1. React+Vite+TS+Shadcn Project
    • Lesson 2. Deploying a React Vite App to Vercel from Vercel CLI
    • Lesson 3 Connecting to GitHub Repo and Automate Deployment
  • Solo Project 3
  • Final Term Project
    • Level 1 Team Project
    • Level 1 Team Project
    • Level 1 Team Project
    • Level 1 Team Project
    • Level 2 Team Project
    • Level 2 Team Project
    • Level 3 Team Project
    • Level 3 Team Project
Powered by GitBook
On this page
  • NEXT.JS Project Setup
  • Prerequisites:
  • Step-by-Step Procedure:
  • Sample Example to Test Your Next.js App:
  • Explanation of the Example:
  • Troubleshooting:
  1. Backend Frameworks-NEXT.JS

Lesson 2: How to set up a new Next.js project

NEXT.JS Project Setup

Prerequisites:

  • Node.js and npm (or yarn or pnpm): Ensure you have Node.js installed on your system. Node.js comes with npm (Node Package Manager). You can download it from the official Node.js website (nodejs.org). Yarn and pnpm are alternative package managers that you can install separately if you prefer.

Step-by-Step Procedure:

  1. Create a New Next.js Project:

    • Open your terminal or command prompt.

    • Navigate to the directory where you want to create your project.

    • Use the create-next-app command to create a new Next.js project. You can use npm, yarn, or pnpm. Here are the commands:

      • npm:

        npx create-next-app@latest nextjs-backend-app
      • yarn:

        yarn create next-app nextjs-backend-app
      • pnpm:

        pnpm create next-app nextjs-app
      • Replace nextjs-backend-app with your desired project name.

    • The command will prompt you with a few questions:

      • Would you like to use TypeScript? (Yes/No) - Choose based on your preference. TypeScript adds static typing to your JavaScript code.

      • Would you like to use ESLint? (Yes/No) - ESLint helps you identify and fix coding style issues. It's recommended to choose "Yes."

      • Would you like to use Tailwind CSS? (Yes/No) - Tailwind CSS is a utility-first CSS framework. Choose based on your styling preference.

      • Would you like to use src/ directory? (Yes/No) - The src/ directory is a common convention for organizing your code. It's generally a good practice to use it, so choose "Yes."

      • Would you like to customize the default import alias? (@/*) (Yes/No) - You can customize the import alias, but the default is fine for most cases.

  2. Navigate to Your Project:

    • Once the project is created, navigate into your project's directory:

      cd my-nextjs-backend-app
  3. Run the Development Server:

    • Start the Next.js development server:

      • npm:

        npm run dev
      • yarn:

        yarn dev
      • pnpm:

        pnpm dev
    • This will start the development server, and you'll see a message indicating that the server is running (e.g., http://localhost:3000).

  4. Open Your Browser:

    • Open your web browser and go to http://localhost:3000. You should see the default Next.js welcome page.

Sample Example to Test Your Next.js App:

Let's create a simple page to test if your Next.js app is working correctly.

  1. Modify the app/page.jsx (or app/page.tsx if you used TypeScript):

    • Open the app/page.jsx (or app/page.tsx) file in your project's directory using a code editor.

    • Replace the existing code with the following:

      JavaScript

      // app/page.jsx
      import React from 'react';
      
      function HomePage() {
        return (
          <div>
            <h1>Welcome to My Next.js App!</h1>
            <p>This is a sample Next.js page.</p>
            <button onClick={() => alert('Button Clicked!')}>Click Me</button>
          </div>
        );
      }
      
      export default HomePage;

      TypeScript

      // app/page.tsx
      import React from 'react';
      
      const HomePage: React.FC = () => {
        return (
          <div>
            <h1>Welcome to My Next.js App!</h1>
            <p>This is a sample page.</p>
            <button onClick={() => alert('Button Clicked!')}>Click Me</button>
          </div>
        );
      };
      
      export default HomePage;
  2. See the Changes:

    • Save the file.

    • Your browser should automatically refresh (if not, refresh it manually).

    • You should now see the heading "Welcome to My Next.js App!", the paragraph "This is a sample Next.js page.", and a button labeled "Click Me."

    • Click the button, and you should see an alert box with the message "Button Clicked!".

Explanation of the Example:

  • We created a simple functional component called HomePage.

  • The component returns JSX, which includes:

    • An <h1> heading.

    • A <p> paragraph.

    • A <button> with an onClick event handler. When the button is clicked, it displays a simple alert box.

  • We exported the HomePage component as the default export, which is how Next.js uses it to render the page.

Troubleshooting:

  • Server Not Running: If you don't see the page in your browser, make sure the development server is running in your terminal. If it's not, start it using npm run dev, yarn dev, or pnpm dev.

  • Errors in the Browser: If you see errors in your browser's console, check your code for syntax errors or other issues.

  • Port Conflicts: If the server fails to start because port 3000 is in use, you can try running the server on a different port using npm run dev -- -p <port_number>, yarn dev -- -p <port_number>, or pnpm dev -- -p <port_number>. Replace <port_number> with the desired port (e.g., 3001).

By following these steps, you can successfully set up a new Next.js project and test it with a simple example. This provides a solid foundation for your Next.js development journey.

PreviousLesson 1: Key Concepts of NodeJS and Express for Backend Web DevelopmentNextLesson 3: How to create Layouts and Pages

Last updated 3 months ago