JavaScript Application Configuration (Node.js): dotenv, convict, nconf Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

JavaScript Application Configuration (Node.js): Best Libraries and Practices



Any serious Node.js application requires a flexible and secure configuration system. Database passwords, API keys, server ports, logging levels — all these parameters should be separated from the code, easily changeable in different environments (development, testing, production), and, most importantly, not end up in the version control system.



In this article, we will look at three of the most popular libraries for managing configuration in Node.js: dotenv, convict, and nconf. You will learn how to properly configure applications, avoid common mistakes, and choose the right tool for your tasks.



1. dotenv — Simplicity and Security



dotenv is the most popular library for loading environment variables from a .env file. Its main advantage is minimalism. You simply create a .env file, add your parameters there, and the library loads them into process.env.



Installation and Basic Usage



npm install dotenv


Create a .env file in the project root:



PORT=3000DB_HOST=localhostDB_USER=adminDB_PASS=supersecretNODE_ENV=development


At the very beginning of your application (usually in app.js or index.js), add:



require('dotenv').config();

const express = require('express');const app = express();

const port = process.env.PORT || 3000;

app.listen(port, () => { console.log(`Server started on port ${port}`);});


Important Rules for Working with dotenv



  • Never commit the .env file to Git. Add it to .gitignore.
  • Create a .env.example file with a template of all variables (without real values). This will help new developers get up to speed quickly.
  • For production, use real environment variables set on the server. dotenv is only needed for development.
  • Do not store complex data structures in .env — only strings and numbers.


2. convict — Validation and Schemas



If dotenv solves the "how to load" problem, then convict answers the question "how to validate and structure". This library allows you to define a configuration schema with data types, default values, and validation rules.



Installation and Schema Example



npm install convict


const convict = require('convict');

const config = convict({ env: { doc: 'Application environment', format: ['production', 'development', 'test'], default: 'development', env: 'NODE_ENV' }, server: { port: { doc: 'HTTP server port', format: 'port', default: 3000, env: 'PORT' } }, db: { host: { doc: 'Database host', format: String, default: 'localhost', env: 'DB_HOST' }, password: { doc: 'Database password', format: String, default: '', env: 'DB_PASS', sensitive: true // do not output in logs } }});

// Load valuesconfig.validate({ allowed: 'strict' });

module.exports = config;


Advantages of convict



  • Automatic validation — if .env contains a string instead of a number, you will get an error at startup.
  • Documentation — the doc field serves as built-in documentation.
  • Security — fields marked sensitive: true are not displayed in logs.
  • Support for nested structures — convenient for complex configurations.


3. nconf — Hierarchy of Sources



nconf

Recommendations