How to Set port in next.js

just need to do:

yarn dev -p PORT_YOU_LIKE

The application will start at http://localhost:3000 by default. The default port can be changed with -p, like so:

 npx next dev -p 4000

Or using the PORT environment variable:

PORT=4000 npx next dev

#note that I used npx not npm

You can also set the hostname to be different from the default of 0.0.0.0, this can be useful for making the application available for other devices on the network. The default hostname can be changed with -H, like so:

 npx next dev -H 192.168.1.2

If you're getting an error that the port is already in use, what you can do to resolve it on windows is

Go to the Task Manager.

Scroll and find a task process named. Node.js: Server-side

End this particular task.

"scripts": {
    "dev": "next dev -p 8080", // for dev 
    "start": "next start -p 8080" // for prod
},

This work for me

 "scripts": { 
       "dev": "next -p 8080" 
},