Is there a way to pass environment variables to the quasar from console?

Viewed 2152

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'
2 Answers

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"

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

Related