Configuration
Configuration means keeping the settings that change between environments, such as the port, database address, mail server or API keys, outside your Express code, and loading the right set when the app starts. You need it as soon as the same app runs on your laptop, a testing server and production, each with different values.
Why not hard-code settings
A line like mongoose.connect('mongodb://localhost/shop') works on your machine and fails everywhere else. Editing code for each server is error-prone, and hard-coded passwords end up in version control where anyone with repository access can see them. Configuration separates the "what" from the "where".
Using the config package
A popular approach in Express projects is the config npm package. It reads JSON files from a config folder and merges them based on the NODE_ENV environment variable you met in the previous lesson.
config/
default.json
development.json
production.json
custom-environment-variables.json
default.json holds shared values. The environment file, chosen by NODE_ENV, overrides only the keys that differ. For example, default.json might contain { "name": "Shop API", "mail": { "host": "localhost" } }, and production.json might change just mail.host.
// index.js
const config = require('config');
const express = require('express');
const app = express();
console.log('App name:', config.get('name'));
console.log('Mail server:', config.get('mail.host'));
if (!config.has('mail.password')) {
console.error('FATAL: mail.password is not defined.');
process.exit(1);
}
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on ${port}`));
config.get() throws an error if the key is missing, which is useful: a typo fails loudly at startup instead of quietly producing undefined.
Keeping secrets in environment variables
Passwords and keys should never be written into those JSON files. Instead, custom-environment-variables.json maps a setting to the name of an environment variable:
{ "mail": { "password": "app_mail_password" } }
When the app starts, the package reads process.env.app_mail_password and places it at mail.password. You set the variable on each server (in a hosting dashboard, a systemd unit, or a shell with export app_mail_password=...), so the secret never enters your repository.
Another common option is the dotenv package, which loads a local .env file into process.env. Node 20 can also load one natively with the --env-file=.env flag. In either case, add .env to .gitignore.
Mistakes that leak or break configuration
- Committing secrets. Once a password is pushed to a shared repository, assume it is exposed and change it. Removing it from a later commit does not erase the history.
- Forgetting NODE_ENV in production. If it is unset, the
configpackage loads development settings, and Express also keeps some development behaviour. - Logging the whole config. A debug line like
console.log(config)can print passwords into log files. - Reading config everywhere. Scattering
config.getcalls across many files makes settings hard to find. Load what you need in one setup module and pass values in.
A sensible checklist
Whichever tool you choose, the goal is the same: one codebase that behaves correctly in every environment just by changing values outside the code.
- Shared, non-secret defaults in one file.
- Per-environment overrides chosen by
NODE_ENV. - Secrets only from environment variables.
- Check required settings at startup and exit with a clear message if any are missing.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.