Get rid of Node.js config grunt work

We recently began using the NPM module config in our Node.js projects.   It's a really nice way to enable our clients to configure their software once we deliver it.  One of the nicest features, IMHO, is its facility that enables you to define custom environment variables for any given config setting.

For example, if your configuration file config/default.json looks like

then you can create another file called config/custom-environment-variables.json like

that allows an administrator to override config values in .json files with environment variables.  For example, if he sets MYAPP_DIRS_UPLOAD to /var/uploads, then config will return /var/uploads instead of /tmp.  Nice, right?

However, for large projects or for lazy developers like me, it's easy to forget to add an entry in config/custom-environment-variables.json whenever you add an entry to config/default.json (or any other config file).

You see, I really hate grunt work.  Since I espouse the invaluable trait of laziness, I created & published an NPM module called config-cev-generator that takes care of creating the config/custom-environment-variables.json file for me.  BTW, "cev" stands for "custom environment variable", which you probably already noticed.  Now, you can have this generated for you!  Just

and you're ready to go.

For example, in a Node.js project that uses config, open a command line in the root of the project and try the command

You'll see all of your config variables ready for use as environment variables printed to stdout.  To save it, just redirect the output to config/custom-environment-variables.json.  For example, given the config file above, cev generates

The default prefix is NODE_APP, separator is _, and environment variables (being environment variables) are upper cased by default.  But wait!  There's more!  cev is nicely configurable:

Invoking

yields

For small projects, who cares, unless your really lazy, right?  But, for large projects, this can become truly annoying.  Consider this config/default.json file:

Writing the config/custom-environment-variables.json for that would just, well, suck.  Check this out:

Nice, huh?

Here's something that might get you bonus points amongst your fellow geeks (read:  lazy programmers).  Add config-cev-generator to your package.json's devDependencies, then invoke the project-local cev command before anything interesting.  For example, consider this package.json fragment:

Now, whenever you run a test or your app, you're sure not to forget to keep your config/custom-environment-variables.json in sync with the rest of your config!  Check this out:

You're welcome. And long live laziness.

The npm module is at https://www.npmjs.com/package/config-cev-generator and the public git repo is at https://github.com/SciSpike/node-config-cev-generator.  Issues welcome; pull requests are more welcome!  🙂

---
Matthew Adams
For more information, please contact info@scispike.com
Lightning Fast Software Development!

No Comments Yet.

Leave a comment