Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 18, 2022 12:40 pm GMT

Environment variables in Nuxt 3

To use environment variables in Nuxt 3, we need to use

runtimeConfig in nuxt.config.

import { defineNuxtConfig } from "nuxt";// https://v3.nuxtjs.org/api/configuration/nuxt.configexport default defineNuxtConfig({  runtimeConfig: {    // The private keys which are only available within server-side    apiSecret: "123",    // Keys within public, will be also exposed to the client-side    public: {      apiBase: process.env.API_BASE || "default_api_url",      otherUrl: process.env.OTHER_URL || "default_other_url"    }  }});

So now if environment variables are present, their values will be stored in apiBase and otherUrl. If environment variables are not present, default values will be used for apiBase and otherUrl.

To access this within components/plugins/server routes use useRuntimeConfig().

<template>  <div>    API Base - {{ runtimeConfig.public.apiBase }} <br />    Other URL -    {{ runtimeConfig.public.otherUrl }} <br />  </div></template><script lang="ts" setup>const runtimeConfig = useRuntimeConfig();</script>

Here is the codesandbox to see it in action,

Official Documentation


Original Link: https://dev.to/amitgurbani/environment-variables-in-nuxt-3-9p6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To