Luckystriike
Luckystriike
NNuxt
Created by Luckystriike on 4/12/2024 in #❓・help
OGImage not showing
No description
66 replies
NNuxt
Created by Luckystriike on 4/3/2024 in #❓・help
[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!
32 replies