1.1.1 • Published 5 years ago

@hellroot/config v1.1.1

Weekly downloads
10
License
MIT
Repository
github
Last release
5 years ago

@hellroot/config

npm dependencies status dev dependencies status code style: prettier

NodeJS application configuration library. It loads your configs as NodeJS modules, not JSONs.

Quick Start

Install package:

npm install @hellroot/config

Create config directory:

project/
  config/
    default.js
    local.js
    development.js
    testing.js
    production.js

Import library and use resulting object defined in default export:

import config from '@hellroot/config';

console.log(config);

Define NODE_ENV and run script:

NODE_ENV=development node index.js

config object will include settings from config/default.js, config/development.js and config/local.js. If there is a conflict of keys, the objects will be expanded, arrays and primitives will be overridden.

Example

default.js

const config = {
  logger: {
    level: 'info'
  },

  server: {
    port: 80,
    ips: ['127.0.0.1']
  }
};

module.exports = config;

development.js

const config = {
  backend: {
    baseUrl: 'https://dev-api.weather.yandex.ru/v1/'
  },

  static: {
    baseUrl: '/static'
  }
};

module.exports = config;

local.js

const config = {
  backend: {
    baseUrl: 'http://127.0.0.1:8090/v1/'
  },

  server: {
    port: 8080,
    ips: ['127.0.0.1', '192.168.1.69']
  }
};

module.exports = config;

Resulting object:

const config = {
  backend: {
    baseUrl: 'http://127.0.0.1:8090/v1/'
  },

  logger: {
    level: 'info'
  },

  server: {
    port: 8080,
    ips: ['127.0.0.1', '192.168.1.69']
  },

  static: {
    baseUrl: '/static'
  }
};

TypeScript integration

You can use this module in TypeScript projects without pain. See example directory.

VSCode autocomplete

One caveat: you will have to describe your configuration manually.