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

2

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'
Holley answered 9/1, 2019 at 11:15 Comment(0)
H
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"

Holley answered 9/1, 2019 at 13:5 Comment(0)
K
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

Katleen answered 24/1, 2019 at 11:41 Comment(3)
Why use quasar in docker? It should be platform independent out of the box no?Holley
This seems like what I want, I am also using with Docker, but how do you access them later?Tello
@Holley 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.Gibbsite

© 2022 - 2024 — McMap. All rights reserved.