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
runtimeConfig: {


public: {
port: process.env.PORT || 3300
}
},
runtimeConfig: {


public: {
port: process.env.PORT || 3300
}
},
10 Replies
kapa.ai
kapa.ai3mo ago
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 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:
PORT=3300 node .output/server/index.mjs

PORT=3300 node .output/server/index.mjs

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:
// This does NOT control the server port!
export default defineNuxtConfig({
runtimeConfig: {
public: {
port: process.env.PORT || 3300 // This is just a variable in your app, not the server port
}
}
})

// This does NOT control the server port!
export default defineNuxtConfig({
runtimeConfig: {
public: {
port: process.env.PORT || 3300 // This is just a variable in your app, not the server port
}
}
})

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:
PORT=3300 node .output/server/index.mjs
PORT=3300 node .output/server/index.mjs
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: ----------------------
kapa.ai
kapa.ai3mo ago
<@1321238874634195004> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Thick Black W
Thick Black WOP3mo ago
what kapa.ai says doesn't work
KirbyTwister
KirbyTwister3mo ago
Try NITRO_PORT instead
Thick Black W
Thick Black WOP3mo ago
did, it only works in dev
KirbyTwister
KirbyTwister3mo ago
Should also work on build
No description
Thick Black W
Thick Black WOP3mo ago
I'm using windows, npm could that be the issue?
KirbyTwister
KirbyTwister3mo ago
potentially more windows, than npm It doesn't matter what package manager you use
Thick Black W
Thick Black WOP3mo ago
this suggestion works on windows
No description
Thick Black W
Thick Black WOP3mo ago
it's a windows thing, on the server ill have to use a dif cmd

Did you find this page helpful?