5

I develop a quasar application and I use 'dotenv' plug-in to parse .env file. Sometimes I need to make provisional changes in variables such as use a different address of back-end for only current run and I don't want to change .env file. If there is a way to pass environment variable in console?

e.g.

quasar dev BACKEND='http://localhost'
za-ek
  • 467
  • 5
  • 11

2 Answers2

7

My solution:

In quasar.conf.js:

    const envparser = require('./src/envparser')
    ...
    build: {
          env: envparser(),

    ...

./src/envparser:

    const DotEnv = require('dotenv')
    const parsedEnv = DotEnv.config().parsed

    const argEnv = {}
    process.argv.forEach((v, k) => {
      if (v.indexOf('--env.') === 0) {
        argEnv[v.substring(6)] = process.argv[k + 1]
      }
    })

    module.exports = function () {
      for (let key in parsedEnv) {
        if (typeof parsedEnv[key] === 'string') {
          parsedEnv[key] = JSON.stringify(parsedEnv[key])
        }
      }

      for (let key in argEnv) {
        parsedEnv[key] = JSON.stringify(argEnv[key])
      }

      return parsedEnv
    }

So now you can use .env file for define environment variables, .env.prod and .env.dev for production and development mode and those will be overwritten by console arguments:

quasar dev --env.VARIABLE "Value"

za-ek
  • 467
  • 5
  • 11
1

My solution:

In my Dockerfile, i export my environment variables

ENV PROTOCOL='https://'

ENV HOST='example.com.br'

ENV PORT=443

then, I get the variables in process.env in my file config/index.js

something like this:

module.exports = {
  NODE_ENV: '"production"',
  PROTOCOL: JSON.stringify(process.env.THRUST_PROTOCOL),
  HOST: JSON.stringify(process.env.THRUST_HOST),
  PORT: JSON.stringify(process.env.THRUST_PORT)
}

I hope to help

Rafael Alves
  • 103
  • 1
  • 1
  • 6
  • Why use quasar in docker? It should be platform independent out of the box no? – za-ek Jan 24 '19 at 14:07
  • This seems like what I want, I am also using with Docker, but how do you access them later? – Beatscribe Mar 01 '21 at 20:16
  • @za-ek Quasar itself is platform independent, but Docker makes a lot of sense when it comes to deployment, for example when you have a full stack web application you would like to want to use docker-compose to build docker images for your backend API, your quasar frontend app, etc., which then easily can be deployed to any docker host. – David Wolf Apr 25 '22 at 13:58