Use the dotenv edit utilities when you need to update a .env* file in place without destroying comments, spacing, ordering, unknown lines, or line endings.
import { editDotenvText } from '@karmaniverous/get-dotenv';
const next = editDotenvText('A=1\n# keep\nB=2\n', {
A: 'updated',
UNUSED: null,
});
import { editDotenvFile } from '@karmaniverous/get-dotenv';
await editDotenvFile(
{ API_URL: 'https://example.com', UNUSED: null },
{
paths: ['./'],
scope: 'env',
privacy: 'private',
env: 'dev',
dotenvToken: '.env',
privateToken: 'local',
},
);
paths only (directories).
searchOrder: 'reverse' (default): last path wins.searchOrder: 'forward': first path wins.<target>.<templateExtension> exists (default template), it is copied first.{ path, createdFromTemplate, changed }.scope: 'global', privacy: 'public' → <dotenvToken>scope: 'env', privacy: 'public' → <dotenvToken>.<env>scope: 'global', privacy: 'private' → <dotenvToken>.<privateToken>scope: 'env', privacy: 'private' → <dotenvToken>.<env>.<privateToken>If scope: 'env' and neither env nor defaultEnv can be resolved, editDotenvFile throws.
null deletes key assignment lines (default).undefined skips (default).For advanced use:
parseDotenvDocument(text) → DotenvDocumentapplyDotenvEdits(doc, updates, opts?) → DotenvDocumentrenderDotenvDocument(doc, eolMode?) → string