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

[Feature request] Write to dotenv file #818

Closed
aswamy opened this issue Mar 4, 2024 · 1 comment
Closed

[Feature request] Write to dotenv file #818

aswamy opened this issue Mar 4, 2024 · 1 comment

Comments

@aswamy
Copy link

aswamy commented Mar 4, 2024

Currently, the dotenv library exposes the following methods:

  • config
  • parse
  • populate
  • decrypt

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:

  • If the value contains a new-line, enclose the variable value in double quotes
  • If the string contains double quotes, use single quotes; if the string contains single & double quotes, use tilde
  • dotenv.patch should be able to ignore comments and bad characters in the file
@motdotla
Copy link
Owner

motdotla commented Mar 4, 2024

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

@motdotla motdotla closed this as completed Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants