· 4 Min read

How to Get a Next.js Website Up and Running

A Step-by-Step Guide

Next.js is a popular React framework that enables you to build server-side rendered and statically generated web applications. This guide will walk you through the process of setting up a Next.js website from scratch.

Prerequisites

Before we begin, make sure you have the following installed on your system:

  1. Node.js (version 12 or later)
  2. npm (Node Package Manager, usually comes with Node.js)

You can check if you have these installed by running the following commands in your terminal:

node --version
npm --version

If you need to install Node.js, visit the official Node.js website and download the appropriate version for your operating system.

Step 1: Create a New Next.js Project

  1. Open your terminal and navigate to the directory where you want to create your project.

  2. Run the following command to create a new Next.js project:

    npx create-next-app@latest my-nextjs-website
    

    Replace my-nextjs-website with your desired project name.

  3. You'll be prompted with some configuration options. For this guide, we'll use the following settings:

    • Would you like to use TypeScript? › No
    • Would you like to use ESLint? › Yes
    • Would you like to use Tailwind CSS? › No
    • Would you like to use src/ directory? › No
    • Would you like to use App Router? › Yes
    • Would you like to customize the default import alias? › No

    Feel free to adjust these settings based on your preferences.

  4. Once the installation is complete, navigate into your project folder:

    cd my-nextjs-website
    

Step 2: Explore the Project Structure

Your Next.js project will have the following structure:

my-nextjs-website/
├── app/
│   ├── favicon.ico
│   ├── globals.css
│   ├── layout.js
│   └── page.js
├── public/
│   ├── next.svg
│   └── vercel.svg
├── .eslintrc.json
├── .gitignore
├── next.config.js
├── package-lock.json
└── package.json

The app directory contains your application code, including the main page.js file which represents your home page.

Step 3: Run the Development Server

To start the development server and see your website in action:

  1. Run the following command in your project directory:

    npm run dev
    
  2. Open your web browser and navigate to http://localhost:3000. You should see the default Next.js welcome page.

Step 4: Customize Your Website

Now that your Next.js website is up and running, you can start customizing it:

  1. Open app/page.js in your code editor.

  2. Modify the content within the export default function Home() function. For example:

    export default function Home() {
      return (
        <main>
          <h1>Welcome to My Next.js Website!</h1>
          <p>This is a custom homepage.</p>
        </main>
      );
    }
  3. Save the file and check your browser. The changes should be reflected immediately thanks to Next.js's hot reloading feature.

Step 5: Add New Pages

To add new pages to your Next.js website:

  1. Create a new file in the app directory. For example, app/about.js.

  2. Add the following code to the new file:

    export default function About() {
      return (
        <div>
          <h1>About Us</h1>
          <p>This is the about page of our Next.js website.</p>
        </div>
      );
    }
  3. You can now access this page at http://localhost:3000/about.

Step 6: Build and Deploy

When you're ready to deploy your website:

  1. Build your application for production:

    npm run build
    
  2. Start the production server:

    npm run start
    

Your Next.js website is now ready for deployment. You can deploy it to various platforms such as Vercel (created by the Next.js team), Netlify, or any other hosting service that supports Node.js applications.

Conclusion

Congratulations! You now have a Next.js website up and running. This is just the beginning – Next.js offers many more features like API routes, static site generation, and more. As you continue to develop your website, refer to the official Next.js documentation for more advanced topics and best practices.

Happy coding!