nuxt / module-builder

Complete solution to build and ship Nuxt modules.
MIT License
212 stars 22 forks source link

Providing environnement variables when application start. #237

Closed guirak closed 3 months ago

guirak commented 3 months ago

Hello,

I have a monorepos nuxt 3 app with several custom nuxt modules.

On these modules, I have defined some environnement variables in my module.ts like :

nuxt.hook("ready", async nuxt => {
runtimeConfig.public.mmiEnvAppHost=(process.env.MMI_ONE_APP_HOST || 'localhost:3000')

This work well in dev but when building the app for production, the hook is called during the build phase and not at the startup of the app (node .output/server/index.mjs).

I want to integrate my app in a kubernetes pod with a config map in different environments so I need to be able to provide dynamically the environnement variables when starting the nuxt server and not during the build.

Is there a hook or another way to do this in a nuxt module ?

danielroe commented 3 months ago

Modules are build-time only. If you want to run something when your app starts you can use/add a nitro plugin.

guirak commented 3 months ago

OK thank you. I have found a way setting the variables in a pinia store from a plugin and using the hydration to have access to the variables in the client side.