Node Konfig

Flexible configuration library for Node.js

Get started View integrations

Configuration library for Node.js written in TypeScript. Can load and merge configuration for multiple sources into one object for easy access. Inspired by Go's Konfig.open in new window

# Quick look

# configs/default.yaml
name: "my-app"
1
2
// configs/development.json
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "user": "development",
    "password": "development"
  }
}
1
2
3
4
5
6
7
8
9
import * as Konfig from "@willsoto/node-konfig-core";
import { VaultLoader } from "@willsoto/node-konfig-vault";
import * as path from "path";

const configDir = path.join(__dirname, "configs");

// Create the store, this is the object you will use to access your config
export const store = new Konfig.Store();

// Always load some defaults.
// These values will be merged with subsequent loaders. On conflicts, last one wins.
store.regiserLoader(
  new Konfig.FileLoader({
    files: [
      {
        path: path.join(configDir, "default.yaml"),
        // This will tell the `FileLoader` how it should interpret the files it loads
        parser: new Konfig.YAMLParser(),
      },
    ],
  }),
);

// Use a local json file in development
if (process.env.NODE_ENV === "development") {
  const loader = new Konfig.FileLoader({
    files: [
      {
        path: path.join(configDir, "development.json"),
        parser: new Konfig.JSONParser(),
      },
    ],
  });

  // Make sure the loader is registered on the store in order for it to be processed
  store.registerLoader(loader);
} else {
  // Use Vault in production or any other non-development environment.
  store.regiserLoader(
    new VaultLoader({
      secrets: [
        {
          key: "secret/data/database",
        },
      ],
    }),
  );
}

// `init` must be called in order to actually resolve and process all the registered loaders
// This only needs to be called once at some point during application startup
await store.init();

store.get("name"); // "my-app"
store.get("database"); // Will return the appropriate database configuration based on `NODE_ENV`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55