N
Nuxt15h ago
JakeT

Multi Line Runtime Config from Env

I am trying to load a certificate from runtime config. It works fine locally (windows), but when i deploy it (dockerized) the runtime config value is set to empty string.
nuxt.config.js
export default defineNuxtConfig({
...
runtimeConfig: {
pg: {
ssl: {
ca: ''
}
}
}
...
})
nuxt.config.js
export default defineNuxtConfig({
...
runtimeConfig: {
pg: {
ssl: {
ca: ''
}
}
}
...
})
.env
NUXT_PG_SSL_CA="-----BEGIN CERTIFICATE-----
...
...
...
-----END CERTIFICATE-----"
.env
NUXT_PG_SSL_CA="-----BEGIN CERTIFICATE-----
...
...
...
-----END CERTIFICATE-----"
console.log({ config: useRuntimeConfig().pg }) local:
{ pg: { ssl: { ca: '-----BEGIN CERTIFICATE-----...' }}}
{ pg: { ssl: { ca: '-----BEGIN CERTIFICATE-----...' }}}
production:
{ pg: { ssl: { ca: '' }}}
{ pg: { ssl: { ca: '' }}}
4 Replies
kapa.ai
kapa.ai15h ago
Hi @JakeT. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Your issue is a common one when working with environment variables and Nuxt's runtimeConfig, especially with multi-line values like certificates. Why it works locally but not in production - Locally: Nuxt CLI automatically loads your .env file during development and build, so NUXT_PG_SSL_CA is available to your config and runtimeConfig as expected. - Production: After building, Nuxt does not read the .env file anymore. Instead, it expects environment variables to be set by the hosting environment (e.g., Docker, cloud provider, etc.). If you only mount or copy the .env file into your container, it will not be read at runtime. You must explicitly set the environment variable in your Docker or deployment configuration for it to be available at runtime. This is by design for compatibility with serverless and edge environments Nuxt .env docs. Multi-line environment variables in Docker Multi-line values (like certificates) can be tricky in Docker. If you use a .env file, Docker Compose does not handle multi-line values well. Instead, you should set the environment variable directly in your Docker configuration, using YAML's | block style or by passing it as a single line with \n characters. Example (docker-compose.yml):
services:
nuxt-app:
environment:
NUXT_PG_SSL_CA: |
-----BEGIN CERTIFICATE-----
...
...
-----END CERTIFICATE-----
services:
nuxt-app:
environment:
NUXT_PG_SSL_CA: |
-----BEGIN CERTIFICATE-----
...
...
-----END CERTIFICATE-----
Or, if you must use a single line:
environment:
NUXT_PG_SSL_CA: "-----BEGIN CERTIFICATE-----\n...\n...\n-----END CERTIFICATE-----"
environment:
NUXT_PG_SSL_CA: "-----BEGIN CERTIFICATE-----\n...\n...\n-----END CERTIFICATE-----"
Correct usage in nuxt.config Your nuxt.config.js should look like this:
export default defineNuxtConfig({
runtimeConfig: {
pg: {
ssl: {
ca: '' // default, will be overridden by NUXT_PG_SSL_CA
}
}
}
})
export default defineNuxtConfig({
runtimeConfig: {
pg: {
ssl: {
ca: '' // default, will be overridden by NUXT_PG_SSL_CA
}
}
}
})
Nuxt will automatically override this value with the NUXT_PG_SSL_CA environment variable at runtime, as long as it is set in the environment where the server runs—not just in a .env file Nuxt runtimeConfig docs. Summary - In production, set NUXT_PG_SSL_CA as an environment variable in your deployment environment, not just in a .env file. - For Docker, use the environment: block in your compose file, and handle multi-line values carefully. - Do not rely on .env files being read at runtime in production. For more details, see the official docs on environment variables in production and runtime config. If you follow these steps and still see an empty string, double-check how your environment variable is being set in your production environment. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai15h ago
<@997592566155464806> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
JakeT
JakeTOP14h ago
the rest of my envs load into runtime config as expected @kapa.ai
kapa.ai
kapa.ai14h ago
Let me check the knowledge base for you again. One moment! :books: Sorry @JakeT, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?

Did you find this page helpful?