Sling Academy
Home/Next.js/How to Run Next.js on Custom Ports

How to Run Next.js on Custom Ports

Last updated: January 14, 2023

By default, a Next.js app will run on port 3000 (both in the development and production environments). However, you can use another port if you like. This short and straight-to-the-point article will show you how to do so.

Modifying the package.json file

Open the package.json file in the root directory of your Next.js project, find the scripts section, and add -p [your desired port] to next dev (development) and next start (production), like this:

"scripts": {
    "dev": "next dev -p 15000",
    "build": "next build",
    "start": "next start -p 18000",
    "lint": "next lint"
}

With this setup, the app will run on port 15000 in development and on port 18000 in production.

Screenshot:

Adding the port when running the app

If you don’t want to change your package.json file, you can add your custom port when you run the npm run dev or npm start commands.

Development:

npm run dev -- -p 15000

Screenshot:

Production mode:

npm start -- -p 20000

That’s it. Thanks for reading!

Previous Article: How to mock Next.js router in Jest tests

Series: Next.js: Configuration & Deployment

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported