dotenv doesn't take console environment variable #14586
-
What happened?I used quasar-vite application and tried to use dotenv. the .env file loaded normally but when I pass them as console params doesn't take priority. What did you expect to happen?as per the vite documentation, it should load as per this docs Hence, when we pass the console environment variable should take whatever from there and ignore the .env and so on. Reproduction URLHow to reproduce?
FlavourQuasar CLI with Vite (@quasar/cli | @quasar/app-vite) AreasQuasar CLI Commands/Configuration (@quasar/cli | @quasar/app-webpack | @quasar/app-vite) Platforms/BrowsersNo response Quasar info outputNo response Relevant log outputNo response Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
You are passing the build: {
env: require('dotenv').config().parsed
} So, if Vite documentation about the environment is for Vite applications. Your application is a Quasar CLI with Vite application, so you must refer to Quasar docs for that. Quasar doesn't handle // Env variable names from .env file or process.env.* to safely pass down to the application code
const envVariableNamesToPass = ['TEST_CONFIG']
// Unless you pass `override: true`, this will not override process.env.* but add new stuff to it
const envFromFile = require('dotenv').config().parsed
const envToPass = {}
for (const name of envVariableNamesToPass) {
// process.env.* contains stuff from regular environment variables
// and the ones loaded by dotenv (dotenv has less priority by its default behavior)
envToPass[name] = process.env[name]
}
// You can use ALL variables via process.env in this file(i.e. quasar.config.js),
// but only the ones specified in `envVariableNamesToPass` will be available in your app code(e.g. IndexPage.vue)
// ...
build: {
env: envToPass
} |
Beta Was this translation helpful? Give feedback.
-
Now I'm able to get console params but the file configuration is not added. I think its related to the above foreach |
Beta Was this translation helpful? Give feedback.
-
Just a minor changes to fix your foreach // Env variable names from .env file or process.env.* to safely pass down to the application code
const envVariableNamesToPass = ['TEST_CONFIG']
// Unless you pass `override: true`, this will not override process.env.*
const envFromFile = require('dotenv').config().parsed
const envToPass = {}
for (const name of Object.keys(envFromFile)) {
envToPass[name] = process.env[name] ?? envFromFile[name]
}
// You can use ALL variables via process.env in this file(i.e. quasar.config.js),
// but only the ones specified in `envVariableNamesToPass` will be available in your app code(e.g. IndexPage.vue)
// ...
build: {
env: envToPass
} |
Beta Was this translation helpful? Give feedback.
You are passing the
dotenv
result as-is:So, if
.env
file is missing, or you are passing env variables manually (e.g.TEST_CONFIG=FROMCONSOLE quasar dev
), they won't be included inrequire('dotenv').config().parsed
. Since you are not handling the other case, it won't work.Vite documentation about the environment is for Vite applications. Your application is a Quasar CLI with Vite application, so you must refer to Quasar docs for that. Quasar doesn't handle
.env
files itself, it leaves it to your liking. In this case, you are using a hybrid dotenv and regular env setup, so you must handle it accordingly. Here is how you would do it:// E…