dotenv-module icon indicating copy to clipboard operation
dotenv-module copied to clipboard

System vars should overwrite dotenv vars

Open z0w13 opened this issue 4 years ago • 9 comments

What problem does this feature solve?

In most places where dotenv is used system env vars override the dotenv vars, however dotenv-module does the exact opposite, swapping this around would give the expected result when using systemvars: true

This feature request is available on Nuxt community (#c28)

z0w13 avatar Oct 15 '19 16:10 z0w13

Does it work as expected with systemvars: true?

avxkim avatar Nov 05 '19 07:11 avxkim

Just tested now in nuxt 2.10, nuxt.config.ts:

  modules: [
    ['@nuxtjs/dotenv', { systemvars: true }]
  ],

It has no effect, i'm setting env variables in a compose file, inside a container they are set, but inside my nuxt app they don't work. So as workaround we have to upload our .env file??

avxkim avatar Nov 05 '19 07:11 avxkim

Same here, even with systemvars: true the system variables (from Heroku in my case) are not taken into account. So when I deploy to Heroku (git push heroku master) as my .env file is gitignored, and my env var from Heroku are not used, the Nuxt production build is not using them (they are undefined when using process.env to access them).

mmorainville avatar Nov 12 '19 13:11 mmorainville

After looking at the source code, it might be because of the return here:

try {
    accessSync(envFilePath, constants.R_OK)
  } catch (err) {
    logger.warn(`No \`${options.filename}\` file found in \`${options.path}\`.`)
    return
  }

Which prevents if (options.systemvars) block to be executed.

mmorainville avatar Nov 12 '19 14:11 mmorainville

Sorry for spamming but I also just noticed that if you set your system variables in the env: {} property of your nuxt.config.js file, the system variables ARE taken into account even without an .env file being there. So this may be a workaround for this issue, or maybe it even is the expected behavior?

mmorainville avatar Nov 13 '19 08:11 mmorainville

Also experiencing this issue, trying to understand why my env variables were working in development - I use a .env file with docker compose - but not in review - Docker build with --build-arg, ARG + ENV before build.

My workaround is to RUN touch .env just before running yarn build so the module stops complaining. And my variables are present in my build :+1:

superbiche avatar Nov 26 '19 13:11 superbiche

+1 for this feature request - just as originally raised by @z0w13 (I'd almost argue this is a bug..)

Example: https://codesandbox.io/s/fervent-fire-o6dcu see package.json modified

  "scripts": {
    "dev": "MINIMAL=1 CSB=1 TEST2=TEST2_OVERRIDE TEST3=TEST3 nuxt",

Expected: (both server/client console.log)

/home | fetch() | from server
/home | fetch() | process.env.TEST1 TEST1
/home | fetch() | context.env.TEST1 TEST1
/home | fetch() | process.env.TEST2 TEST2_OVERRIDE
/home | fetch() | context.env.TEST2 TEST2_OVERRIDE
/home | fetch() | process.env.TEST3 TEST3
/home | fetch() | context.env.TEST3 TEST3

Actual: (both server/client console.log)

/home | fetch() | from server
/home | fetch() | process.env.TEST1 TEST1
/home | fetch() | context.env.TEST1 TEST1
/home | fetch() | process.env.TEST2 TEST2
/home | fetch() | context.env.TEST2 TEST2
/home | fetch() | process.env.TEST3 TEST3
/home | fetch() | context.env.TEST3 TEST3

Would a PR be accepted?

hartmut-co-uk avatar May 03 '20 15:05 hartmut-co-uk

+1 I think this bug report and this "feature-request" are talking about the same issue. The problem is that when deploying an application, the environment variables are ignored since there isn't a .env file in the deployment like there is locally. Correct me if I'm wrong.

stuft2 avatar Aug 11 '20 20:08 stuft2

Any updates here? Seems problematic to not be able to override the .env with system vars.

marr avatar Jul 14 '22 18:07 marr