Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access environment variables in a composable in Nuxt3?

In my nuxt 3 App, for data fetching I wanna set a baseURL for all my API calls. As I get this baseURL from enviroment variable. How to set the the baseURL?

I warp the useFetch with composables, but then I can't get the baseURL as useRuntimeConfig() is not accessable there.

// My composables function
const baseURL = "how to get baseURL from process.env";

export const myFetch = async (url: string) => {
  const options = {
    baseURL: baseURL,
  };

  return await useFetch(url, options);
}
like image 896
nur_riyad Avatar asked Nov 27 '25 12:11

nur_riyad


1 Answers

You can access the runtime config by doing so

export default () => {
  const config = useRuntimeConfig()

  console.log(config)
}

As shown here: https://github.com/nuxt/framework/discussions/3215#discussioncomment-3088206

like image 200
kissu Avatar answered Nov 30 '25 04:11

kissu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!