Project Setup

Setting Up a Next.js Project

This guide will help you create a new Next.js project and set up a folder structure using the app directory with two pages: homepage and payments.

Prerequisites

Before you begin, ensure you have Node.js and npm installed on your machine. You can download them from nodejs.org (opens in a new tab). Assuming you are already a nodejs developer, You should already have those installed

Step 1: Create a New Next.js Project

Start by creating a new Next.js project if you don’t have one set up already. The most common approach is to use Create Next App (opens in a new tab). We will create a project named, Tech For Charity.

Terminal
npx create-next-app@latest tech-for-charity --typescript --eslint

Navigate to the created project

Terminal
cd tech-for-charity

Project structure

We will create a simple donation project to showcase mpesa express (STK PUSH) integration. For these we will need only two pages, the homepage to handle the payment and a payments to show the donations

Inside the tech-for-charity directory, set up the following folder structure:

tech-for-charity/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ payments/
β”‚   β”‚   └── page.tsx
β”‚   β”œβ”€β”€ layout.tsx
β”‚   └── page.tsx
β”‚
|── components/
β”œβ”€β”€ public/
β”‚   └── favicon.ico
β”‚
β”œβ”€β”€ styles/
β”‚   β”œβ”€β”€ globals.css
β”‚
β”œβ”€β”€ .gitignore
β”œβ”€β”€ next.config.js
β”œβ”€β”€ package.json
└── README.md

We will also setup Tailwind css to handle styling - This is optional though.

If you already chose tailwindcss when bootstrapping your nextjs project, You dont have to go through the tailwindcss setup since it will be setup by default

If You dont have tailwindcss already setup, You can follow the below steps.

TailwindCss Installation & Setup

Install tailwindcss and its peer dependencies via npm, and then run the init command to generate both tailwind.config.js and postcss.config.js.

Terminal
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Add the paths to all of your template files in your tailwind.config.js file.

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
 
    // Or if using `src` directory:
    "./src/**/*.{js,ts,jsx,tsx,mdx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

Add the @tailwind directives for each of Tailwind’s layers to your globals.css file.

global.css
@tailwind base;
@tailwind components;
@tailwind utilities;

Optionally Run the project to ensure everything is setup properly

Terminal
npm run dev

confirm if tailwind css is well setup by using any of its directives.

page.ts
export default function Home() {
  return (
    <h1 className="text-3xl font-bold underline">
      Hello From Mpesa Intergration By Rizwan
    </h1>
  )
}