How to set production port?
npm run build always builds on default port 3000 despite setting the PORT .env variable, and even getting help from chatgpt that said to set it in runtime config
10 Replies
Hi @Thick Black W. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To set the production port for your Nuxt app, you do not set it in
Set the port by exporting
runtimeConfig
or in your .env
as NUXT_PUBLIC_PORT
or similar. Instead, Nuxt's production server (the one you run after npm run build
) reads the port from the environment variables NITRO_PORT
or PORT
at runtime.
Key points:
- The runtimeConfig
is for application-level config, not for controlling the server's listening port.
- The .env
file is only read during development, build, and generate. In production, you must set environment variables in your hosting environment or via the command line.
- Setting port
in runtimeConfig.public
will not affect the server port; it only exposes a value to your app code.
How to set the production port:
1. When starting your production server, set the PORT
or NITRO_PORT
environment variable:
Or, if you use a process manager like PM2, set the port
in your ecosystem config or as an environment variable.
2. Do not set the port in runtimeConfig
:
3. If you want to use a .env
file for local production preview, use nuxt preview
:
- nuxt preview
will load .env
and respect the PORT
variable for previewing your built app locally, but in real production you must set the environment variable yourself (docs).
References:
- Nuxt .env and production environment variables
- Nuxt deployment and server port
- Nuxt preview command and .env usage
Summary:Set the port by exporting
PORT
or NITRO_PORT
in your environment when starting the server, not in runtimeConfig
. For example:
This will make your Nuxt app listen on port 3300 in production.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1321238874634195004> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
what kapa.ai says doesn't work
Try NITRO_PORT instead
did, it only works in dev
Should also work on build

I'm using windows, npm
could that be the issue?
potentially
more windows, than npm
It doesn't matter what package manager you use
this suggestion works on windows

it's a windows thing, on the server ill have to use a dif cmd