by Sai gowtham

How to change a port number in Next.js app

In this tutorial, we are going to learn how to change a default port number in the next.js app.

Note: If we create a new next app using the create-next-app cli, by default the app runs on the port 3000.

Changing the Port number

  1. Open the next app in your favorite code editor.
  2. Navigate to the package.json file and add the -p flag with your desired port number to the following command.
"dev": "next dev -p 5000",

Here I added a port number 5000.

  1. Now, run the development server using npm run dev command your app will start on the port number you specified in step 2.
➜  nextjs-blog git:(master)npm run dev

> learn-starter@0.1.0 dev /Users/saigowtham/Desktop/nextjs-blog
> next dev -p 5000

[ wait ]  starting the development server ...
[ info ]  waiting on http://localhost:5000 ...
Browserslist: caniuse-lite is outdated. Please run next command `npm update`
[ ready ] compiled successfully - ready on http://localhost:5000

If you want to change a port number temporarily, then you can use the following command.

npm run dev -- --p 5002

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course