Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/htdocs/w008e48d/sigadev/wordpress/wp-includes/locale.php on line 375
Configuration in Node.js | Things I do

Configuration in Node.js

Posted on by 0 comment

UsingĀ  configuration settings is quite easy in Node. Just use a plain old object.

Or store the configuration as JSON-File:

BUT…. It’s not always that easy.

Typical requirements are:

  • multiple config files (with hierarchical dependencies)
  • config needs to vary based on running environment (development, production)
  • use environment variables
  • use arguments
  • including credentials and sensitive information in source can be problematic

Take a closer look at NPM and you realize there are many packages.

After reading a few posts I decided to focus in convict and nconf.

I started with nconf.

Description from their website:

Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging. it is designed to be a simple key-value store with support for both local and remote storage

nconf makes it easier to handle configuration files but the advantages are not as many as I expected. Ok, it’s better using a well tested module with community support than doing it myself but nconf was not able to convince me.

So I moved on to convict.

After spending 5 minutes for the documentation it was clear, it is the right thing because it has a validation feature.

Before you can use validation, you have to prepare a schema. In order to help detect misconfiguration, convict allows you to define a format for each setting:

  • Type information
  • Default values
  • values from environmental variables or command-line arguments
  • Documentation (nice feature, so even after 10 months you still know what a setting is good for :-) )

So due to its validation feature, illegal config values are detected and environmental variables can be mapped to any configuration key without coding.

So I stay with convict.

photo credit: JaulaDeArdilla via photopin cc


Category: Things I do

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">