NuxtN
Nuxt3mo ago
4 replies
dervondenbergen

Overriding Variable in Dev mode

I am trying to find out, wether there is a way on how I can override a environment variable in Nuxt.

I already looked through the docs (https://nuxt.com/docs/4.x/guide/going-further/runtime-config#environment-variables) and the GitHub issues of Nuxt and Nitro, but can't find anyone with the same issue.

We are using layers and define the layer when building multiple sites:

const { SITE = "site1" } = process.env;

export default defineNuxtConfig({
    extends: [`./sites/${SITE}`, "./base"],
    …
});


That way running nuxt dev or nuxt build uses by default site1 as the layer.

We then have NPM scripts like npm run dev:site1 SITE=site1 nuxt dev and npm run dev:site2 SITE=site2 nuxt dev.

The problem is now, that defining a ENV variable like that isn't supported in Windows. There is the cross-env package however I was wondering, wether it is possible or would make sense to override variables via a flag.

My basic idea was something like nuxt dev -e SITE=site1, similar to how it works with docker. That way it works independent of OS and wouldn't need extra packages.
Nuxt
Nuxt provides a runtime config API to expose configuration and secrets within your application.
Runtime Config · Nuxt Advanced v4
Was this page helpful?