DEV Community

Cover image for How to set port in Next.js
collegewap
collegewap

Posted on • Updated on • Originally published at codingdeft.com

How to set port in Next.js

By default, when you run npm run dev, it will start the Next.js app in port 3000. There can be times when port 3000 is used by another application or you have a requirement to start the server in another port.
We will see how to achieve it in this article.

Creating a Next.js Project

First, create a new Next.js project using the following command:

npx create-next-app@latest next-set-port
Enter fullscreen mode Exit fullscreen mode

Updating the port

Now let's say you want to change the port to 8090. You can open the package.json and update the dev script as follows:

"dev": "next dev -p 8090",
Enter fullscreen mode Exit fullscreen mode

Now if you run npm run dev, you should be able to access the app at http://localhost:8090.

Setting the port for production build

If you want to run the project in a different port after it is built (after running npm run build),
you can do so by updating the start script:

"start": "next start -p 8090",
Enter fullscreen mode Exit fullscreen mode

Now you can run npm start to see the page served at port 8090.

Top comments (0)