Nuxt prepare set dotenv path
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...![No description](https://cdn.answeroverflow.com/1118561580938969148/image.png)
11 Replies
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
![No description](https://cdn.answeroverflow.com/1119008160586407956/image.png)
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 flagBut 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
scriptsthe line in nuxt.config.ts
![No description](https://cdn.answeroverflow.com/1120398420063031337/image.png)
@Joni 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.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 scriptsHi @Joni can I see your full docker-compose.yml and Dockerfile? My proxy url is not working on my Docker env, but if I just run it via "yarn dev" then it works. Thanks