Skip to content

How to set port in Next.js

Jun 25, 2024Abhishek EH1 Min Read
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:

1npx create-next-app@latest next-set-port

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:

1"dev": "next dev -p 8090",

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:

1"start": "next start -p 8090",

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

If you have liked article, do follow me on twitter to get more real time updates!

Leave a Comment

© 2024 CodingDeft.Com