"Gracefully handle
.env
files."
npm install dotenv-extra
import DotEnv from 'dotenv-extra';
// Load a specific .env file
const dotEnv = new DotEnv('path-to-file');
// Update or insert an entry
dotEnv
.upsert('key', 'value')
.upsert('foo', 'bar')
.upsert('baz', 'qux');
// Get the current entries as an object
const entries = dotEnv.dump();
console.log(entries);
// { key: 'value', foo: 'bar', baz: 'qux', ... }
// Save the changes back to the file
const success = dotEnv.save();
if (!success) {
console.error('Failed to save changes');
}
npx dotenv-extra [<key> <value>...]
- Expects a
.env
file to be present and writable. - File creation is intentionally not implemented.
2-liner example of quickly setting up a project:
cp .env.example .env
npx dotenv-extra APP_KEY 12345 APP_ENV production MAINTENANCE true