#Export const from module for use in app nuxt.config

4 messages · Page 1 of 1 (latest)

zinc sparrow
#

How do i export a const of eg. favicons from a module to apps, that use the module, so the favicons can be set inside the apps nuxt.config?

astral crescentBOT
#

Hi @zinc sparrow. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To export a constant like favicons from a Nuxt module for use in apps that use the module, you can utilize the runtimeConfig option in your module's setup function. Here's how you can approach this:

  1. In your Nuxt module, you can define the constant (e.g., favicons) and add it to the runtimeConfig.public object:
import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'

export default defineNuxtModule({
  setup(options, nuxt) {
    const favicons = [
      // Your favicon definitions here
    ]

    // Add favicons to public runtime config
    nuxt.options.runtimeConfig.public.favicons = favicons
  }
})
  1. In the app using your module, you can access this constant using the useRuntimeConfig composable:
<script setup>
const runtimeConfig = useRuntimeConfig()
const favicons = runtimeConfig.public.favicons
</script>

This approach leverages the runtimeConfig feature of Nuxt, which allows you to expose configuration and constants from your module to the rest of the application Docs > Guide > Going-further > Runtime Config (https://nuxt.com/docs/guide/going-further/runtime-config).

Remember that anything added to runtimeConfig.public will be available on both the client and server sides of your Nuxt application. If you need to keep the constant private to the server side only, you can omit the public key and add it directly to runtimeConfig.

Also, keep in mind that the values in runtimeConfig should be serializable, as mentioned in the documentation Docs > Guide > Going-further > Runtime Config (https://nuxt.com/docs/guide/going-further/runtime-config#serialization).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@zinc sparrow Kapa.ai is still learning and improving, please let me know how I did by reacting below.