Skip to content
Latchkey

Nuxt "useRuntimeConfig" misuse outside a Nuxt context in CI

Nuxt composables like useRuntimeConfig() rely on the active Nuxt instance. Calling them at module top level, in a plain utility, or outside a setup/handler means no instance is bound, so they throw during the build or render.

What this error means

A build, prerender, or test step fails with "[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function" referencing useRuntimeConfig.

nuxt
[nuxt] A composable that requires access to the Nuxt instance was called outside of a
plugin, Nuxt hook, Nuxt middleware, or Vue setup function.
  at useRuntimeConfig (node_modules/nuxt/dist/app/...)

Common causes

The composable runs at module top level

Calling useRuntimeConfig() at the top of a file (not inside setup or a handler) executes before any Nuxt instance is available.

Used in a plain utility helper

A shared helper imported by many places calls the composable outside any Nuxt context, so the instance is unavailable.

How to fix it

Call it inside setup or a handler

Move the call into a component setup, a plugin, or a server route handler where the Nuxt instance exists.

server/api/posts.ts
// server/api/posts.ts
export default defineEventHandler((event) => {
  const config = useRuntimeConfig(event);
  return $fetch(config.apiBase + '/posts');
});

Pass config in instead of calling it in helpers

Read the config at the call site and pass values to plain utilities rather than calling the composable inside them.

app.vue
// component setup
const config = useRuntimeConfig();
buildUrl(config.public.apiBase);

How to prevent it

  • Call Nuxt composables only in setup, plugins, middleware, or handlers.
  • Pass config values into plain utilities rather than calling composables there.
  • On the server, pass event to useRuntimeConfig(event).

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →