next.js: way to configure hostname and port through next.config.js

Viewed 3926

i am using next.js 9.5.x and I am looking for a way to configure hostname and port through next.config.js. I already searched the documentation about this, but I did not find a answer for this topic.

I read already a series of .env files in the next.config.js file for setting up serval things and passing some of the environment variables to the application code itself. I got already some environment variables specifying the hostname and port and would like to reuse them to setup the hostname and port for the server / development-server.

The only two solutions I found, for changing these two parameters, are the command line settings of next, or having a custom server.js. At the moment I am using a custom server.js and inside I read all .env files again, just for having the port and hostname setup.

I want to get rid of the server.js, since I believe that there must be a way for configuring this through the next.config.js file, I have anyway, and where everything is already in place.

Thank you for some information about this.

2 Answers

To change port:

In package.josn, => scripts => add "dev": "next dev -p 5500"

"scripts": {
    "dev": "next dev -p 5500",
    "build": "next build",
    "start": "next start"
  }

enter image description here

*I could not find how to change host name.

You can use -H in order to specify the host. This is the help message from next dev:

Description
        Starts the application in development mode (hot-code reloading, error
        reporting, etc)

      Usage
        $ next dev <dir> -p <port number>

      <dir> represents the directory of the Next.js application.
      If no directory is provided, the current directory will be used.

      Options
        --port, -p      A port number on which to start the application
        --hostname, -H  Hostname on which to start the application (default: 0.0.0.0)
        --help, -h      Displays this message
Related