#Nuxt prepare set dotenv path

14 messages · Page 1 of 1 (latest)

granite ibex
#

How do I set the dotenv path for "postinstall": "nuxt prepare?
Doesn't work -> "postinstall": "nuxt prepare --dotenv ../.env"
Picture is right after using npm install, at nuxt.config.ts:32:114 I'm using process.env.PUBLIC_API_BASE_URL which doesn't exist due to not using the right dotenv path...

lament panther
#

The prepare command doesn’t support the dotenv flag. What are trying to achieve?

granite ibex
#

Thanks for your answer, sorry for my late reply. I'm trying to load my .env file from the folder above my webapp

#

I don't want multiple .env files due to double occurrence of the same env var (Redundancy)

lament panther
granite ibex
#

idk exactly what it does, but when I do npm install, the npm run postinstall (fromthe package.json; nuxt prepare) gets called and the npm install errors. If that happens while starting my docker container and setting everything up, the frontend might fail

#

It does also fail with the --dotenv ../.env flag, removing the "postinstall": "nuxt prepare --dotenv ../.env" line in package.json lets npm install work without issues - so the actual problem is, that the prepare command doesn’t support the dotenv flag

lament panther
#

But more to the issue, it shouldn’t fail really. Perhaps there’s a configuration issue? Maybe related to how you’re accessing env variables that cannot be read somewhere where they shouldn’t and the prepare command is failing as a consequence?

granite ibex
#

the line in nuxt.config.ts

lament panther
#

@granite ibex Therein lies your problem. The config references a variable that may not exist and by using non-null assertion you’re claiming it always will. Env variables should always have a fallback.

During prepare the config is evaluated to infer types etc. Env variables don’t have a particular use at this point. Therefore that particular line will be evaluated regardless. I would use a ternary or nullish coalescing operator and return a default value.

granite ibex
#

Thanks for clearance, problem is, I can't set a default value, since I dont know what domain/url this webpage and my backend will be hosted on

#

Guess I need to remove postinstall from the package.json scripts