Environment and configuration are supposed to be and should be, independent of the application code. Not only does it conform to the best practices, but it also provides a clear gateway to set development, staging, and production environments separately.
The project uses the dotenv npm package for injecting environment variables into the app. According to its documentation, it’s better to include it during the startup command line script than to include it in the application code - a complete separation of configuration. In production environments, process managers such as PM2 can load the configuration from the environment by using this package as well.
In the src/config folder, I store the configuration for each dependency separately in its own file. As the application grows, it provides a clean way of organizing different configuration settings for different npm packages. Modules that require dynamic configuration data, such as mongoose, can directly access the environment variables made available to the app via dotenv. I find it very helpful to declare any configuration-related stuff within the config folder and not anywhere else in the code.
You might have noticed that the app file within src/config isn’t related to any particular npm package. This is because it’s used to define application-specific configurations that can later be used by the business logic layer for various purposes. For example, the jwtSecret variable in the app configuration file can be used to sign and verify tokens during authentication and authorization.
According to the 12FactorApp best practices, application logs are to be managed by the executing environment. However, for the purpose of keeping this project simple, I’ve configured the app to generate logs in a separate directory.
In the next article, I’ll be going deep into the folder structure and the various ways in which I organize my business logic code while writing REST APIs.
Copyright © 2026. Salman Ahmad.