NuxtN
Nuxt2y ago
5 replies
kue

How to access environment variables or runtime config outside of Nuxt context?

Hi folks!

I need to access the environment variable outside of the Nuxt context like this:
// lib/http.js

// process.env undefined
// can't use `useRuntimeConfig` outside nuxt context
const http = axios.create({ ... });
const strapiHttp = axios.create({ ... });

// services/blog.service.js
import { strapiHttp } from "~/lib";

export const getBlogPosts = async () => {
  const res = await strapiHttp.get(...);
  return res.data;
};

<script setup>
const { data } = useAsyncData(() => getBlogPosts());
</script>


I have several instance of axios and this is the current structure. Is there a way to preserve this kind of structure without having to rewrite it to composables in order to access env variables or runtime config?

Thank you!
Was this page helpful?