motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.01k stars 853 forks source link

[Feature request] Write to dotenv file #818

Closed aswamy closed 6 months ago

aswamy commented 6 months ago

Currently, the dotenv library exposes the following methods:

I'd love to be able to write to the .env file, or patch it with existing variables.

Example:

const envVars = {
  API-KEY: "555",
  SECRET: 123
}

dotenv.write(envVars, file); // Writes the provided object into the .env file
const envVarsPatch = {
  API-KEY: "444",
}

dotenv.patch(envVarsPatch, file); // If the environment variables provided in the object exist in the file, update it; or else insert it

Some edge case considerations:

motdotla commented 6 months ago

this is outside of the scope of this library, but it will be added to dotenvx - which adds a lot of improved tooling and safety around .env files.

likely it will be dotenvx set KEY=value