
How To Run Next JS App
Next.js is one of the most popular React frameworks for building fast, SEO-friendly, and scalable web applications. Whether you’re developing a personal portfolio, an eCommerce platform, or a corporate website, knowing how to run a Next.js app efficiently is crucial for your workflow. This guide will walk you through everything from initial setup to deployment, ensuring that your Next.js project runs smoothly in development, production, and server environments.
In this comprehensive guide, we’ll cover the following topics:
- Setting up a Next.js project
- Running a Next.js app in development mode
- Building and running a production build
- Running a Next.js app on a server
- Using environment variables
- Common issues when running a Next.js app
- Best practices for smooth operation
By the end, you’ll understand the complete process of running and managing a Next.js application effectively.
What is Next.js?
Before diving into the setup and running process, let’s understand what Next.js is.
Next.js is a React framework developed by Vercel. It simplifies the development of web applications by offering powerful features out of the box, such as:
- File-based routing
- Server-Side Rendering (SSR)
- Static Site Generation (SSG)
- API Routes
- Image Optimization
- Built-in TypeScript support
- Incremental Static Regeneration (ISR)
These features make Next.js a go-to choice for developers looking to build fast, SEO-friendly applications.
Prerequisites for Running a Next.js App
Before you can run a Next.js application, ensure that your environment meets the following requirements:
-
Node.js (version 18 or higher)
You can verify installation using:node -v -
npm or yarn (package managers)
- Check npm version:
npm -v - Check yarn version:
yarn -v
- Check npm version:
-
Code editor – VS Code is highly recommended.
Once these prerequisites are in place, you’re ready to create and run your Next.js app.
Setting Up a New Next.js Application
To start, you can create a new Next.js application using the official setup command.
Step 1: Create a Next.js App
Open your terminal and run the following command:
npx create-next-app@latest my-next-app
You can also use Yarn:
yarn create next-app my-next-app
This command will prompt you for some configuration options such as TypeScript, ESLint, and Tailwind CSS. Once the setup completes, you’ll have a ready-to-use Next.js boilerplate.
Step 2: Navigate to the Project Directory
cd my-next-app
Step 3: Start the Development Server
Run the development server using:
npm run dev
Or if you’re using Yarn:
yarn dev
Your app will now be running on http://localhost:3000.
You’ll see a default Next.js homepage confirming that the setup was successful.
Running a Next.js App in Development Mode
When you run the command npm run dev, Next.js launches your app in development mode. This mode comes with useful features such as:
- Hot Reloading – Automatically refreshes the app when code changes.
- Detailed error messages – Helps debug during development.
- Linting and type checks – Ensures code quality.
If you make any changes to your components, the browser updates automatically without needing a manual refresh.
Example: Editing the Homepage
Open pages/index.js and modify the code:
export default function Home() {
return <h1>Welcome to My Next.js App!</h1>;
}
Save the file, and you’ll immediately see the changes reflected in your browser.
Running a Next.js App in Production Mode
Once your application is ready, you’ll need to run it in production mode for optimal performance. This involves building the app and then starting it.
Step 1: Build the Application
Use the following command to generate a production build:
npm run build
This command compiles your project, optimizes assets, and prepares static pages for production. It outputs a .next directory containing the optimized build files.
Step 2: Start the Production Server
After building, run the production server using:
npm start
Or with Yarn:
yarn start
Your app will now be live at http://localhost:3000, but it’s running in optimized production mode.
Step 3: Testing the Production Build
To ensure everything is working correctly:
- Navigate to
http://localhost:3000in your browser. - Test all routes and pages.
- Check the console for any warnings or errors.
Running Next.js on a Custom Port
By default, Next.js runs on port 3000. However, you can specify a custom port using:
npm run dev -- -p 4000
Or for production:
npm start -- -p 8080
Your app will now run on the port you specify.
Environment Variables in Next.js
Next.js supports environment variables that allow you to manage configuration settings without hardcoding them into your code.
Setting Up Environment Variables
Create a .env.local file in your project root directory:
NEXT_PUBLIC_API_URL=https://api.example.com
Access the variable in your code:
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
console.log(apiUrl);
Remember that environment variables prefixed with NEXT_PUBLIC_ are available on the client side.
Running Next.js in a Production Server Environment
In real-world scenarios, you’ll often run a Next.js app on a dedicated server or hosting provider.
Step 1: Install Dependencies on the Server
npm install
Step 2: Build the App
npm run build
Step 3: Run the Server
npm start
This process is similar to local production but may vary based on your hosting provider.
Step 4: Configure Process Manager (PM2)
To keep your Next.js app running continuously, use PM2, a process manager for Node.js:
npm install pm2 -g
pm2 start npm --name "nextjs-app" -- run start
You can monitor the app using:
pm2 logs
pm2 status
This ensures your app restarts automatically if the server reboots or crashes.
Running a Next.js App with Docker
You can containerize your Next.js application using Docker for consistent environments across development and production.
Example Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
Build and Run Docker Container
docker build -t nextjs-app .
docker run -p 3000:3000 nextjs-app
Your app will now be running inside a Docker container on port 3000.
Common Issues When Running a Next.js App
Even with proper setup, you might encounter issues while running your Next.js app. Let’s discuss some common ones.
1. Port Already in Use
Error: Error: listen EADDRINUSE: address already in use
Solution: Use a different port with -p flag:
npm run dev -- -p 4001
2. Missing Dependencies
Error: Cannot find module 'react'
Solution: Reinstall dependencies:
npm install
3. Build Fails in Production
Error: Missing environment variables or TypeScript issues.
Solution: Check .env files and verify TypeScript configuration.
4. API Routes Not Working
Ensure that your API files are placed correctly in the /pages/api directory.
Example:
pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: 'Hello from API route!' });
}
Best Practices for Running Next.js Apps
- Keep Dependencies Updated: Regularly update packages to avoid compatibility issues.
- Use Environment Variables Securely: Never expose sensitive keys on the client side.
- Use PM2 or Docker for Production: This ensures consistent and stable runtime environments.
- Enable Image Optimization: Use Next.js
<Image />component for better performance. - Use Static Generation When Possible: It improves page speed and SEO.
- Monitor Performance: Use analytics and monitoring tools to track application performance.
Deploying a Next.js App
You can deploy your Next.js app to various platforms, including:
- Vercel (official hosting for Next.js)
- Netlify
- AWS / Google Cloud / Azure
- Custom VPS using PM2 or Docker
Deployment typically involves pushing your code to GitHub and connecting your repository to one of these platforms.
Example (Vercel Deployment):
npm install -g vercel
vercel
Follow the prompts, and your app will be live in seconds.
Conclusion
Running a Next.js app efficiently involves understanding its different modes — development, production, and server environments. Whether you’re testing locally or deploying to production, Next.js provides a streamlined process that makes running and maintaining your app simple and effective.
By mastering these commands, setup methods, and best practices, you can ensure your Next.js application performs optimally and is easy to manage across all environments.
If you’re looking for expert help in building, running, or scaling your Next.js or MERN stack applications, AAMAX — a full-service digital marketing and web development company offering MERN Stack Development, Web Development, SEO, and Digital Marketing Services. Their experienced developers can help you create efficient, secure, and high-performance web applications tailored to your business needs.






