samtgarson / nuxt-env

Inject env vars for your Nuxt app at runtime
170 stars 15 forks source link

Use env within nuxt.config.js #20

Closed vacijj closed 5 years ago

vacijj commented 5 years ago

Is it possible to access the ENV vars within the nuxt.config.js?

Doing it this way the process.env.*variables get replaced during the build.

    [ 'nuxt-env', {
      keys: [
        { key: 'DOMAIN_DE' },
        { key: 'DOMAIN_AT' },
        { key: 'NUXT_I18N_DIFFERENT_DOMAINS' },
        { key: 'NUXT_I18N_FORWARDED_HOST' }
      ]
    } ],
    [ 'nuxt-i18n', {
      differentDomains: process.env.NUXT_I18N_DIFFERENT_DOMAINS || false,
      forwardedHost: process.env.NUXT_I18N_FORWARDED_HOST || false,
      locales: [
        {
          code: 'de_DE',
          iso: 'de-DE',
          name: 'DE',
          domain: process.env.DOMAIN_DE
        },
        {
          code: 'de_AT',
          iso: 'de-AT',
          name: 'AT',
          domain: process.env.DOMAIN_AT
        }
      ],
      vueI18n: {
        fallbackLocale: 'de_DE'
      },

      strategy: 'prefix'
    } ],

Any idea how I could get this to work?

samtgarson commented 5 years ago

Are you running your app in universal mode?

If so, you should use dotenv on the first line of your nuxt config to load in your .env file.

mikl commented 5 years ago

I'm also looking for a way to do this. dotenv can't do it, since my environment vars are not from a .env file, but rather defined in Kubernetes.

aaalekseev commented 5 years ago

Guys, did you solve this issue somehow? Is it possible to read env-variables coming from Kubernetes/Docker-compose?

xinu commented 5 years ago

Are you running your app in universal mode?

If so, you should use dotenv on the first line of your nuxt config to load in your .env file.

dotenv is loading .env files on runtime? in my tests it seems that it is backing it during build

samtgarson commented 5 years ago

@xinu it loads them whenever you run the dotenv line. Using nuxt-env should mean your env vars aren't baked in at build time but it depends on your config and how you're using the module

samtgarson commented 5 years ago

@mikl @aaalekseev if you're using kubernetes and you have env vars injected into your deployment then they should be available on process.env

syffs commented 5 years ago

@mikl @aaalekseev if you're using kubernetes and you have env vars injected into your deployment then they should be available on process.env

@samtgarson accessing env vars at runtime from process.env is one thing, but how do you make sure they're actually resolved at runtime instead of build time within nuxt.config.js ?

To run Nuxt.js in a microservice environment, you need to be able to configure about everything (like axios proxy, base url, etc...) at runtime...

@mikl @vjanoch @aaalekseev have you solved this somehow ?

samtgarson commented 5 years ago

@syffs I'm not sure I follow, if use this library with your app in Universal mode your env vars shouldn't get resolved at build time (although whatever is in your process.env at build time will be used, and the same for runtime)

Could you provide a little more information about your setup?

I'm still not quite clear on what this issue is reporting. Could anyone provide a simple reproduction?

alcaprar commented 5 years ago

Having the same issue here.

We set some env vars in nuxt-env inside nuxt.config.js and we would like to use those vars inside nuxt.config.js. Something like the following:

router: {
  base: this.$env.NUXT_BASE_DIR // this var should be resolved at runtime and not build time
}
samtgarson commented 5 years ago

@alessandrocaprarelli that wouldn't be possible, as the nuxt.config.js file is a plain js file that is run before Nuxt is even instantiated, but you can just set the router base to be process.env.NUXT_BASE_DIR, no?

samtgarson commented 5 years ago

The original issue is actually an issue with nuxt-i18n, not nuxt-env. If you're having issues with other libraries baking their configuration in at build time, you need to raise an issue with those libraries, therefore I'm closing this issue.

@vjanoch @mikl @aaalekseev @syffs we use this library in kubernetes in production without any issues. If you're having issues with baked environment variables, please ensure the issue is with nuxt-env and not with a different third party library—if it is, feel free to open another issue.

alcaprar commented 5 years ago

@samtgarson The problem is that the environment variable is picked up at build time (run in our CI system) and not at runtime.

samtgarson commented 5 years ago

@alessandrocaprarelli If you're referring to the router base, then that's Nuxt behaviour and there's nothing this library can do about that. I'd suggest looking at a module to modify the router options at runtime, or raise an issue with Nuxt directly.