# envconf

> a module for express-style programmatic configuration

Latest version **1.1.0** (published 2014-05-23) · 0 weekly downloads

## Install

```sh
npm install envconf
pnpm add envconf
yarn add envconf
bun add envconf
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2014-05-23 |
| First published | 2013-06-14 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Microsoft Corporation |
| Maintainers | ctavares |
| Keywords | config, configuration, environment |

## Links

- npm: https://www.npmjs.com/package/envconf
- Repository: git@github.com:Azure/envconf
- Homepage: https://github.com/Azure/envconf
- Issues: http://github.com/Azure/envconf/issues
- npm.io page: https://npm.io/package/envconf

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2014-05-23
- 1.0.0 — 2014-05-02
- 0.0.4 — 2013-07-26
- 0.0.3 — 2013-07-25
- 0.0.2 — 2013-07-09
- 0.0.1 — 2013-06-14

## README

envconf
===========

This module makes it easy to use express-style configuration for any application.
It allows your users to define separate configuration environments in code
and switch between sets of configuration via a single environment variable.

## Basic Usage

```javascript

var envconf = require('envconf');

var c = envconf.createConfig();

c.configure('development', function (c) {
  c.set('settingOne', 'devValue');
});

c.configure('production', function (c) {
  // You can also pass in an object literal to set a bunch
  // of values at once
  c.set({
    settingTwo: 'prodValue'
    prodOnly: 'prodSpecific'
  });
});

c('development').get('settingOne').should.equal('devValue');

process.env.NODE_ENV = 'production';
c.default.get('settingTwo').should.equal('prodValue');
c.default.get('prodOnly').should.equal('prodSpecific');
```

The previous code shows picking up the default environment from the NODE_ENV environment variable.

You can however configure your own environment variables as shown below.

```javascript

var c2 = envconf.createConfig({ defaultEnvVar: 'MY_LIBRARY_VAR'});

c2.configure('development', function (c) {
  c.set('settingOne', 'devValue');
});

c2.configure('production', function (c) {
  c.set('settingTwo', 'prodValue');
});

c2('development').get('settingOne').should.equal('devValue');

process.env.MY_LIBRARY_VAR = 'production';
c.default.get('settingTwo').should.equal('prodValue');
```

## Setting Getters

Instead of a setting a simple value, you can instead use the setFunc method to
provide a function that will run when the value is requested:

```javascript
var c2 = envconf.createConfig();

c2.configure('development', function (c) {
  c.setFunc('settingOne', function () { return 'This value came from a function'; });
});

c2.get('settingOne').should.equal('This value came from a function');
```

or, if you use the object literal version of set, values that are functions
will be treated as if you called setFunc for that value:

```javascript
var c2 = envconf.createConfig();

c2.configure('development', function (c) {
  c.set({
    settingOne: function () { return 'This value also came from a function'; }
  });
});

c2.get('settingOne').should.equal('This value also came from a function');
```

This can be handy if you want to have a default value that you need to derive from
ambient state.

## Customizing the config object

Do you want to add helper methods for your specific configuration? Or set specific
values in every configuration? It's easy with a config customizer:

```javascript

function addConfigHelpers(config) {
  config.useSql = function (host, db) {
    config.set('sql host', host);
    config.set('sql database name', db);
  }
}

var c3 = envconf.createConfig( { customizer: addConfigHelpers });

c3.configure('test', function (c) {
  c.useSql('testmachine', 'testdb');
});

c3.configure('production', function (c) {
  c.useSql('realDatabase', 'actualDb');
});

// Can also customize after creation, customizations
// will affect any child configs too!
c3 = envconf.createConfig();
var prod = c3('prod');

c3.customize(addConfigHelpers);

c3.configure('production', function (c) {
  c.useSql('realDatabase', 'actualDb');
});
```


## Saving and Restoring config values

Are you making changes to a global configuration in your unit tests, and want
to ensure you've restored the state after your test? Use a snapshot:

```javascript

var c4 = envconf.createConfig();
c4.configure(function (c) {
  c.set('originalValue', 'one');
});

// set up contents of c4

var snapshot = c4.snapshot();

c4.configure(function (c) {
  c.set('originalValue', 'two');
});

c4.restore(snapshot);

c4.get('originalValue').should.equal('one');
```

Snapshot/restore also saves and restores any child configurations.

## Temporary Configs

Similarly, you might want to set up a configuration and then be able
to throw it away without giving it a name. Easy:

```javascript

var c5 = envconf.createConfig();
c5.configure(function (c) {
  c.set('originalValue', 'one');
});

var c6 = envconf.tempConfig();
c6.set('tempValue', 'temp');

// You can look up values in the temp config or the parent
c6.get('tempValue').should.equal('temp');
c6.get('originalValue').should.equal('one');

// But the parent has no record of the temp
c5.environments.length.should.equal(0);
```

---
_Source: https://npm.io/package/envconf · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
