Ebooks

Node-config tutorial

Node-config tutorial shows how to create configuration files for Node applications with node-config module.

Node-config

Node-config creates configuration files for application deployments.

Node-config allows us to define a set of default parameters and extend them for different deployment environments (development, qa, staging, production, etc.).

The configuration files are located in the default config directory. The location can be overriden with the NODE_CONFIG_DIR environment variable. The NODE_ENV environment variable contains the name of our application's deployment environment; it is development by default.

Node config supports various configuration file formats, including JSON, YAML, properties, or XML. The default configuration file is default.json (or default.yaml, default.xml). If we use a production deployment, then the configuration is loaded from production.json.

Setting up Node-config

First, we install node-config.

$ node -v
v11.5.0

We use Node version 11.5.0.

$ npm init -y

We initiate a new Node application.

$ npm i config

We install node-config with nmp i config.

$ npm i js-yaml

In addition, we install js-yaml for YAML support.

Node-config example

The following example retrieves configuration data with config package.

config/default.json
{
    "app": {
        "port": 3000
    },
    "db": {
        "host": "localhost",
        "port": 27017,
        "name": "ydb"
    }
}

We have default.json in the config directory.

simple.js
const config = require('config');

let appPort = config.get('app.port');
console.log(`Application port: ${appPort}`);

let dbHost = config.get('db.host');
console.log(`Database host: ${dbHost}`);

let dbPort = config.get('db.port');
console.log(`Database port: ${dbPort}`);

let dbName = config.get('db.name');
console.log(`Database name: ${dbName}`);

console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));

We load the config package and get the values with config.get() function. The default deployment type is specified in NODE_ENV.

$ node simple.js
Application port: 3000
Database host: localhost
Database port: 27017
Database name: ydb
NODE_ENV: development

This is the output.

Node-config example II

We change the configuration file to YAML and set a production deployment environment.

config/default.yaml
app:
  port: 3000

db: 
  host: localhost
  port: 27017
  name: ydb

This is default.yaml file.

config/production.yaml
app:
  port: 3300

db: 
  host: localhost
  port: 27017
  name: mydb

This is production.yaml file.

simple.js
const config = require('config');

let appPort = config.get('app.port');
console.log(`Application port: ${appPort}`);
...

The simple.js file is the same.

$ node simple.js
Application port: 3000
Database host: localhost
Database port: 27017
Database name: ydb
NODE_ENV: development

This is the output for the default environment. The configuration is loaded from default.yaml.

$ set NODE_ENV=production
$ node simple.js
Application port: 3300
Database host: localhost
Database port: 27017
Database name: mydb
NODE_ENV: production

We change the NODE_ENV variable with the set command. (Use export on Linux.) Now the configuration data is loaded from production.yaml file.

In this tutorial, we have used node-config package to create configuration files for our Node.js application.

You might also be interested in the following related tutorials: Node.js tutorial. List all JavaScript tutorials.