[RESOLVED] Nuxt3 and netlify hosting env variables

Hey! First of all "Nuxt3 is epic!". But I need some help with Nuxt 3 Netlify environment variables. I'm using Nuxt 3 in combination with useRuntimeConfig and Mongoose. I'm inserting MongoDB records by calling an API route in my Nuxt server. The environment variable from my .env file works locally but not on Netlify. I did set up the environment variable in the Netlify dashboard. Can anyone tell me why the environment variable is not available? The code also works when hardcoding the connection string, so I'm sure it's an issue with the environment variable. What I tried: - Modified the nuxt.config.ts to add the runtimeconfig export default defineNuxtConfig({ runtimeConfig: { mongoDBURI: process.env.NUXT_ENV_MONGO_URI }, ) - Used useRuntimeConfig in one of my server api files "server/api/map.ts" - Used useRuntimeConfig in my index.ts which was injected in the nitro plugin like: export default defineNuxtConfig({ nitro: { plugins: ["~/server/index.ts"] } } - added the env variables to the netlify dashboard and tried it with a "NUXTENV" prefix Mongoose connect example (works when hardcoding the connectionstring): import {Nitro} from "nitropack" import mongoose from "mongoose" export default async (_nitroApp: Nitro) => { const config = useRuntimeConfig(); try{ var connString = config.mongoDBUR console.log(@@ connecting to db...... ${connString} @@); await mongoose.connect(connString); console.log("@@ connected to mongo db @@"); } catch(ec){ console.error("@@ error connecting to mongoDb @@:", ec); } } Can anyone help me with this issue? Thanks a lot!
26 Replies
pyplacca
pyplacca4mo ago
added the env variables to the netlify dashboard and tried it with a "NUXTENV" prefix
the env key in your Netlify dashboard looks different from what's in you .env file. did you mean to type NUXT_ENV_MONGO_URI as indicated in your env file?
manniL
manniL4mo ago
it should be NUXT_MONGO_DB_URI See https://www.youtube.com/watch?v=_FYV5WfiWvs
Alexander Lichter
YouTube
Nuxt's runtimeConfig - The most common mistake
🤯 Throughout my projects, consultancies and code reviews I came across lots of interesting findings - also with regards to Nuxt's runtimeConfig feature. Repeatedly I noticed ONE big mistake though which you might do at this very moment in your project. In this video, explain what it is, why you shouldn't do it and how to use runtimeConfig correc...
Luckystriike
Luckystriike4mo ago
Sorry both are nuxt_env
pyplacca
pyplacca4mo ago
@Luckystriike, all good now?
Luckystriike
Luckystriike4mo ago
Nope
pyplacca
pyplacca4mo ago
did you redeploy the app on Netlify?
Luckystriike
Luckystriike4mo ago
Did this
No description
pyplacca
pyplacca4mo ago
that usually works
Luckystriike
Luckystriike4mo ago
Let me screenshot stuff for you and send it in the help thread
pyplacca
pyplacca4mo ago
why don't you change the env key to something like VITE_MONGO_URI and see if that works
Luckystriike
Luckystriike4mo ago
I was thinking to change it to something without _?
pyplacca
pyplacca4mo ago
there's no harm in trying
Luckystriike
Luckystriike4mo ago
No description
No description
No description
No description
No description
Luckystriike
Luckystriike4mo ago
Just strange that it works locally with a .env file
pyplacca
pyplacca4mo ago
really strange. could you have possibly forgotten to set the value of the URI on Netlify?
Luckystriike
Luckystriike4mo ago
Nope 🙂 also fully deployed a new domain and didn’t work either Mhh
pyplacca
pyplacca4mo ago
hmmm. sorry I couldn't help u out here hopefully someone else will drop in to assist
Luckystriike
Luckystriike4mo ago
Np it is probably something stupid 😦 but thanks for the help so far
pyplacca
pyplacca4mo ago
welcome
Luckystriike
Luckystriike4mo ago
Fixed it! Used the npm dotenv package, initialized the config in the plugin init function and made a toml build file. Looks like it is working now 😉
pyplacca
pyplacca4mo ago
Oh great! 🚀
manniL
manniL4mo ago
you don't need it at all your problem is still as shown in here ^ if you set runtimeConfig.private.myVar it should be NUXT_PRIVATE_MY_VAR
Luckystriike
Luckystriike4mo ago
I tried renaming the vara to NUXTPRIVATE but didn’t work. Am i doing something wrong here (keep the NUXTPRIVATE rename in mind)? https://discord.com/channels/473401852243869706/1225087322106757172/1225120327554830346
manniL
manniL4mo ago
the right hand side doesn't really matter. The name in the runtimeConfig dictates the name of the env variable. Did you watch the video?
Luckystriike
Luckystriike4mo ago
I also renamed the nuxt.config name. Maybe i missed something. I’ll continue watching the video later today/tomorrow. Hey! I watched your video. I did use the prefix already, but there was another issue. I used… ‘const config = useRuntimeConfig(); config.mongoUri’ But it actually should have been: ‘const config = useRuntimeConfig(); config.private.mongoUri’ Changing this fixed the issue. Thanks for sharing the video and pointing me in the right direction.
manniL
manniL4mo ago
Glad it helped 🙏🏻