How to Set Port in Next.js
If you’re working with Next.js, you might have come across the need to set a specific port for your application. By default, Next.js runs on port 3000, but in some cases, you may want to change it to a different port. In this blog post, we’ll explore two different solutions to set the port in Next.js.
Solution 1: Using the “next.config.js” File
The first solution involves modifying the “next.config.js” file in your Next.js project. This file allows you to configure various aspects of your Next.js application, including the port.
To set a specific port, open the “next.config.js” file and add the following code:
module.exports = {
server: {
port: 8080, // Replace 8080 with your desired port number
},
};
Save the file and restart your Next.js server. Now, your application will be running on the specified port.
Solution 2: Using the “package.json” File
If you prefer to set the port using the “package.json” file, you can do so by modifying the “scripts” section.
Open the “package.json” file and locate the “scripts” section. Add the following code:
"scripts": {
"dev": "next dev -p 8080" // Replace 8080 with your desired port number
},
Save the file and restart your Next.js server. Now, your application will be running on the specified port.
These are the two main solutions to set the port in Next.js. Choose the one that suits your preferences and requirements.
Happy coding!
Leave a Reply