How to specify another directory or name for the .env file in Nuxt
Asked Answered
C

2

2

I don't want to follow the convention of putting my environment variable (.env file) at the root directory of my Nuxt project.

How can I achieve another directory or even name for it without using the @nuxtjs/dotenv module? (I know this one is already baked into Nuxt since v2.13 and hence, not needed anymore).

Cruickshank answered 11/7, 2021 at 16:22 Comment(0)
C
6

Looking at this answer: https://github.com/nuxt/nuxt.js/issues/8331#issuecomment-727533175

You can specify a different location and name if you'd like, with the following

yarn dev --dotenv variables/.env_file

With the following structure

enter image description here

Of course, this will work with yarn generate and yarn build.

Cruickshank answered 11/7, 2021 at 16:22 Comment(2)
ok, so it must be defined on each build and there is no way to define the path in nuxt.config.js. thanksTwitt
@MojtabaBarari IMO, this makes more sense to have it on the build command. Either keep it in the source directory, or if you want to override it, do it on the build yep.Cruickshank
L
1

I got no luck with the --dotenv flag, so I'm just using replacing .env with the desired contents upon build or serve, like cp .env.xxx .env && yarn dev. Not the prettiest, but good enough.

In your case the command would be cp variables/.env_file .env && yarn dev.

Lascar answered 1/7, 2022 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.