Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring your own process.env #55

Closed
moeriki opened this issue May 25, 2021 · 4 comments
Closed

Bring your own process.env #55

moeriki opened this issue May 25, 2021 · 4 comments

Comments

@moeriki
Copy link

moeriki commented May 25, 2021

ignoreProcessEnv: true is great, though undocumented 🙈, because we can prevent mutating process.env.

Sadly this option has the effect that we cannot interpolate variables across different files. Something that is quite convenient in a dotenv setup like .env.local, …, .env.

Ideally this option would be useProcessEnv: boolean | object instead of ignoreProcessEnv: boolean so we can loop over the seperate dotenv files without modifying process.env and without loosing the ability to refer variables across the files.

# .env.local
NAME="Dieter"
# .env
ADMIN="${NAME}"
const dotenv = require('dotenv');
const expand = require('dotenv-expand');

const env = {};
expand(dotenv.config({ path: '.env.local' }), { ignoreProcessEnv: true, useProcessEnv: env });
expand(dotenv.config({ path: '.env' }), { ignoreProcessEnv: true, useProcessEnv: env });

// Expected
// assert.ok(env.ADMIN === 'Dieter');
@motdotla
Copy link
Owner

Yes, I see what you mean. I'll think on this.. maybe a tighter integration/hook with the dotenv module.

In other news, ignoreProcessEnv is now partially documented: https://github.com/motdotla/dotenv-expand#ignoreprocessenv

@WoodyWoodsta
Copy link

I think there is a more fundamental problem with this option, and probably why it wasn't documented in the first place. It does too much.

Not only does it not write to process.env, as the documentation suggests, but it also does not read in values from process.env. This means it cuts out a whole host of use-cases.

I think the original suggestion was a good one, but doesn't completely solve the underlying problem, which is that there should be a way to say to the module: "Read in values from process.env, but don't write them out to it". This can be achieved by making the type of ignoreProcessEnv: boolean | 'read-only'. When set to read-only, expand will read from process.env but not write to it.

Then, consider also adding an option to inject your own process.env, however, this should be achievable using Object.assign or spreading in between each file load.

@WoodyWoodsta
Copy link

@motdotla Any thoughts on the above? Would be great to get some direction on whether this should live in the dotenv module or out here for now.

@motdotla
Copy link
Owner

motdotla commented Feb 10, 2024

you can now bring your own processEnv. will be released as v11.0.0 shortly.

const myObject = {}
const dotenv = {
  processEnv: myObject,
  parsed: {
    SHOULD_NOT_EXIST: 'testing'
  }
}
const obj = dotenvExpand.expand(dotenv).parsed

console.log(obj.SHOULD_NOT_EXIST) // testing
console.log(myObject.SHOULD_NOT_EXIST) // testing
console.log(process.env.SHOULD_NOT_EXIST) // undefined

https://github.com/motdotla/dotenv-expand?tab=readme-ov-file#processenv

(ignoreProcessEnv has been removed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants