N
Nuxt2y ago
Joni

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
11 Replies
Cue
Cue2y ago
The prepare command doesn’t support the dotenv flag. What are trying to achieve?
Joni
JoniOP2y ago
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
Joni
JoniOP2y ago
I don't want multiple .env files due to double occurrence of the same env var (Redundancy)
Cue
Cue2y ago
What are you expecting prepare to do with the env exactly?
Joni
JoniOP2y ago
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
Cue
Cue2y ago
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?
Joni
JoniOP2y ago
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
Joni
JoniOP2y ago
the line in nuxt.config.ts
No description
Cue
Cue2y ago
@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.
Joni
JoniOP2y ago
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
Fonti
Fonti2y ago
Hi @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
Want results from more Discord servers?
Add your server