#Nuxt prepare set dotenv path
14 messages · Page 1 of 1 (latest)
The prepare command doesn’t support the dotenv flag. What are trying to achieve?
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)
What are you expecting prepare to do with the env exactly?
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
But that’s expected behaviour. The env flag is not supported because it does nothing with environment variables, it configures Nuxt for IDE support and such.
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?
Well it does something with env vars, since im using them in my nuxt.config.ts, bc of that I get the error Cannot read properties of undefined (reading 'length') in line 32:114
My env vars are in a folder above the actual nuxt project tho, that's why I added --dotenv ../.env to all of my package.json scripts
the line in nuxt.config.ts
@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.