N
Nuxt3mo ago
Spamtier

Nuxt environment variables load array of objects

I have a runtimeConfig which includes a list of my projects. They are accessible via NUXT_PUBLIC_PROJECTS env variable. Is it possible to load them directly as an object, or do I have to create a composable useProjects() which returns the projects if they are an array, or parses them via JSON.parse if it is a string?
2 Replies
Single
Single3mo ago
You can import a json file in your nuxt.config and make the content available via runtimeConfig:
import config from './yourconfig.json'

// .....

export default defineNuxtConfig({
runtimeConfig: {
public: {
yourKey: config,
},
},
})
import config from './yourconfig.json'

// .....

export default defineNuxtConfig({
runtimeConfig: {
public: {
yourKey: config,
},
},
})
and then use it like:
useRuntimeConfig().public.yourKey.<value>
useRuntimeConfig().public.yourKey.<value>
pyplacca
pyplacca3mo ago
You can use a json file as @Single has stated. However, you don’t need to pass the data to the runtime config. You can simply import it in whichever page or component you wish to access it from