![]() |
Setting a custom port in Next.js is essential when you want to run your development or production server on a port other than the default (3000). While Next.js doesn’t directly support port configuration via .env.local for development, you can control the port using alternative methods. This article will guide you through the process of setting the port in Next.js in detail. PrerequisitesTo set the custom port in next.js we can use the following approaches: Table of Content Steps to Create the Next AppStep 1: Create a NextJS application using the following commandnpx create-next-app my-pp Step 2: Navigate to project directorycd my-app Project Structure:The updated dependencies in package.json file will look like: "dependencies": { Method 1: Using package.jsonIn this approach we will modify the package.json. Here we modify the script section by adding “dev”: “next dev -p 4000“. This will starts the server on port 4000 instead of the default port 3000. Below is the modified script section: "scripts": { Example: Implementation to set the PORT by changing in the package.josn file.
Output: ![]() Output Method 2: Using command-lineIn this approach, we define the port number when launching the Next.js development server via the command npm run dev — -p <port_number>. This will starts the server on specified port number instead of the default port 3000. Example: Implementation to set the PORT with the command.
Output: Run the below command to see the result. npm run dev -- -p <port_number> ![]() Output ConclusionTo set a custom port in Next.js, modify the |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |