N
Nuxt15mo ago
oemer

Can i set `.env` file path inside `nuxt.config.ts`?

I know I can use yarn dev --dotenv ../.env. But I want to define this inside nuxt.config.ts. Is this possible?
6 Replies
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
oemer
oemer15mo ago
Yes that works. But having an option in nuxt.config.ts supports DRY principle. In my case I want to have a base Layer, which imports all .env variable. Layers that extend the base Layer should not have to do this again. Also I think it's more convenient to have all options available in nuxt.config.ts that are available in the package.json, like --dotenv.
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
oemer
oemer15mo ago
Interesting approach! I saw the c12 with setupDotenv part too in the nuxt code. Was hoping for a defined api instead of a "hacky" like solution. I guess I will just add --dotenv ../.env to every script I need. Currently it seems to be the safest and most clean way to accomplish it.
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
oemer
oemer15mo ago
@Pratham Interesting approach that could fix my problem as well. However - similar to what you said - it's just way safer and future proof to do it in each package.json for now. Even if it includes some copy-paste. 😄 We use the .env-file only for local development. In a staging and production deployment, the environment variables are added to the docker container, no .env-file there. I would love to see an easy API:
//nuxt.config.ts
export default defineNuxtConfig({
dotenv: "../.env",
})
//nuxt.config.ts
export default defineNuxtConfig({
dotenv: "../.env",
})
AFAIK, this was possible in earlier versions of Nuxt.