Configuration

Applications often run in different environments. Depending on the environment, different configuration settings should be used. For example, usually the local environment relies on specific database credentials, valid only for the local DB instance. The production environment would use a separate set of DB credentials. Since configuration variables change, best practice is to store configuration variables in the environment.

Externally defined environment variables are visible inside Node.js through the process.env global. We could try to solve the problem of multiple environments by setting the environment variables separately in each environment. This can quickly get unwieldy, especially in the development and testing environments where these values need to be easily mocked and/or changed.

In Node.js applications, it’s common to use .env files, holding key-value pairs where each key represents a particular value, to represent each environment. Running an app in different environments is then just a matter of swapping in the correct .env file.

A good approach for using this technique in Nest is to create a ConfigModule that exposes a ConfigService which loads the appropriate .env file. While you may choose to write such a module yourself, for convenience Nest provides the @nestjs/config package out-of-the box. We’ll cover this package in the current chapter.

Installation

To begin using it, we first install the required dependency.

  1. $ npm i --save @nestjs/config

Hint The @nestjs/config package internally uses dotenv.

Getting started

Once the installation process is complete, we can import the ConfigModule. Typically, we’ll import it into the root AppModule and control its behavior using the .forRoot() static method. During this step, environment variable key/value pairs are parsed and resolved. Later, we’ll see several options for accessing the ConfigService class of the ConfigModule in our other feature modules.

app.module.ts

  1. import { Module } from '@nestjs/common';
  2. import { ConfigModule } from '@nestjs/config';
  3. @Module({
  4. imports: [ConfigModule.forRoot()],
  5. })
  6. export class AppModule {}

The above code will load and parse a .env file from the default location (the project root directory), merge key/value pairs from the .env file with environment variables assigned to process.env, and store the result in a private structure that you can access through the ConfigService. The forRoot() method registers the ConfigService provider, which provides a get() method for reading these parsed/merged configuration variables. Since @nestjs/config relies on dotenv, it uses that package’s rules for resolving conflicts in environment variable names. When a key exists both in the runtime environment as an environment variable (e.g., via OS shell exports like export DATABASE_USER=test) and in a .env file, the runtime environment variable takes precedence.

A sample .env file looks something like this:

  1. DATABASE_USER=test
  2. DATABASE_PASSWORD=test

Custom env file path

By default, the package looks for a .env file in the root directory of the application. To specify another path for the .env file, set the envFilePath property of an (optional) options object you pass to forRoot(), as follows:

  1. ConfigModule.forRoot({
  2. envFilePath: '.development.env',
  3. });

You can also specify multiple paths for .env files like this:

  1. ConfigModule.forRoot({
  2. envFilePath: ['.env.development.local', '.env.development'],
  3. });

If a variable is found in multiple files, the first one takes precedence.

Disable env variables loading

If you don’t want to load the .env file, but instead would like to simply access environment variables from the runtime environment (as with OS shell exports like export DATABASE_USER=test), set the options object’s ignoreEnvFile property to true, as follows:

  1. ConfigModule.forRoot({
  2. ignoreEnvFile: true,
  3. });

Use module globally

When you want to use ConfigModule in other modules, you’ll need to import it (as is standard with any Nest module). Alternatively, declare it as a global module by setting the options object’s isGlobal property to true, as shown below. In that case, you will not need to import ConfigModule in other modules once it’s been loaded in the root module (e.g., AppModule).

  1. ConfigModule.forRoot({
  2. isGlobal: true,
  3. });

Custom configuration files

For more complex projects, you may utilize custom configuration files to return nested configuration objects. This allows you to group related configuration settings by function (e.g., database-related settings), and to store related settings in individual files to help manage them independently.

A custom configuration file exports a factory function that returns a configuration object. The configuration object can be any arbitrarily nested plain JavaScript object. The process.env object will contain the fully resolved environment variable key/value pairs (with .env file and externally defined variables resolved and merged as described above). Since you control the returned configuration object, you can add any required logic to cast values to an appropriate type, set default values, etc. For example:

config/configuration.ts

  1. export default () => ({
  2. port: parseInt(process.env.PORT, 10) || 3000,
  3. database: {
  4. host: process.env.DATABASE_HOST,
  5. port: parseInt(process.env.DATABASE_PORT, 10) || 5432
  6. }
  7. });

We load this file using the load property of the options object we pass to the ConfigModule.forRoot() method:

  1. import configuration from './config/configuration';
  2. @Module({
  3. imports: [
  4. ConfigModule.forRoot({
  5. load: [configuration],
  6. }),
  7. ],
  8. })
  9. export class AppModule {}

Notice The value assigned to the load property is an array, allowing you to load multiple configuration files (e.g. load: [databaseConfig, authConfig])

Official enterprise support

  • Configuration - 图1 Providing technical guidance
  • Configuration - 图2 Performing in-depth code reviews
  • Configuration - 图3 Mentoring team members
  • Configuration - 图4 Advising best practices

Explore more

Using the ConfigService

To access configuration values from our ConfigService, we first need to inject ConfigService. As with any provider, we need to import its containing module - the ConfigModule - into the module that will use it (unless you set the isGlobal property in the options object passed to the ConfigModule.forRoot() method to true). Import it into a feature module as shown below.

feature.module.ts

  1. @Module({
  2. imports: [ConfigModule],
  3. // ...
  4. })

Then we can inject it using standard constructor injection:

  1. constructor(private configService: ConfigService) {}

And use it in our class:

  1. // get an environment variable
  2. const dbUser = this.configService.get<string>('DATABASE_USER');
  3. // get a custom configuration value
  4. const dbHost = this.configService.get<string>('database.host');

As shown above, use the configService.get() method to get a simple environment variable by passing the variable name. You can do TypeScript type hinting by passing the type, as shown above (e.g., get<string>(...)). The get() method can also traverse a nested custom configuration object (created via a Custom configuration file), as shown in the second example above. The get() method also takes an optional second argument defining a default value, which will be returned when the key doesn’t exist, as shown below:

  1. // use "localhost" when "database.host" is not defined
  2. const dbHost = this.configService.get<string>('database.host', 'localhost');

Configuration namespaces

The ConfigModule allows you to define and load multiple custom configuration files, as shown in Custom configuration files above. You can manage complex configuration object hierarchies with nested configuration objects as shown in that section. Alternatively, you can return a “namespaced” configuration object with the registerAs() function as follows:

config/database.config.ts

  1. export default registerAs('database', () => ({
  2. host: process.env.DATABASE_HOST,
  3. port: process.env.DATABASE_PORT || 5432
  4. }));

As with custom configuration files, inside your registerAs() factory function, the process.env object will contain the fully resolved environment variable key/value pairs (with .env file and externally defined variables resolved and merged as described above).

Hint The registerAs function is exported from the @nestjs/config package.

Load a namespaced configuration with the load property of the forRoot() method’s options object, in the same way you load a custom configuration file:

  1. import databaseConfig from './config/database.config';
  2. @Module({
  3. imports: [
  4. ConfigModule.forRoot({
  5. load: [databaseConfig],
  6. }),
  7. ],
  8. })
  9. export class AppModule {}

Now, to get the host value from the database namespace, use dot notation. Use 'database' as the prefix to the property name, corresponding to the name of the namespace (passed as the first argument to the registerAs() function):

  1. const dbHost = this.configService.get<string>('database.host');

A reasonable alternative is to inject the database namespace directly. This allows us to benefit from strong typing:

  1. constructor(
  2. @Inject(databaseConfig.KEY)
  3. private dbConfig: ConfigType<typeof databaseConfig>,
  4. ) {}

Hint The ConfigType is exported from the @nestjs/config package.

Partial registration

Thus far, we’ve processed configuration files in our root module (e.g., AppModule), with the forRoot() method. Perhaps you have a more complex project structure, with feature-specific configuration files located in multiple different directories. Rather than load all these files in the root module, the @nestjs/config package provides a feature called partial registration, which references only the configuration files associated with each feature module. Use the forFeature() static method within a feature module to perform this partial registration, as follows:

  1. import databaseConfig from './config/database.config';
  2. @Module({
  3. imports: [ConfigModule.forFeature(databaseConfig)],
  4. })
  5. export class DatabaseModule {}

Warning In some circumstances, you may need to access properties loaded via partial registration using the onModuleInit() hook, rather than in a constructor. This is because the forFeature() method is run during module initialization, and the order of module initialization is indeterminate. If you access values loaded this way by another module, in a constructor, the module that the configuration depends upon may not yet have initialized. The onModuleInit() method runs only after all modules it depends upon have been initialized, so this technique is safe.

Schema validation

It is standard practice to throw an exception during application startup if required environment variables haven’t been provided or if they don’t meet certain validation rules. The @nestjs/config package enables use of the Joi npm package to support this type of validation. With Joi, you define an object schema and validate JavaScript objects against it.

Install Joi (and its types, for TypeScript users):

  1. $ npm install --save @hapi/joi
  2. $ npm install --save-dev @types/hapi__joi

Notice The latest version of @hapi/joi requires you to be running Node v12 or later. For older versions of node, please install v16.1.8. This is mainly after the release of v17.0.2 which causes errors during build time. For more information, please refer to their documentation & this github issue.

Now we can define a Joi validation schema and pass it via the validationSchema property of the forRoot() method’s options object, as shown below:

app.module.ts

  1. import * as Joi from '@hapi/joi';
  2. @Module({
  3. imports: [
  4. ConfigModule.forRoot({
  5. validationSchema: Joi.object({
  6. NODE_ENV: Joi.string()
  7. .valid('development', 'production', 'test', 'provision')
  8. .default('development'),
  9. PORT: Joi.number().default(3000),
  10. }),
  11. }),
  12. ],
  13. })
  14. export class AppModule {}

By default, all schema keys are considered optional. Here, we set default values for NODE_ENV and PORT which will be used if we don’t provide these variables in the environment (.env file or process environment). Alternatively, we can use the required() validation method to require that a value must be defined in the environment (.env file or process environment). In this case, the validation step will throw an exception if we don’t provide the variable in the environment. See Joi validation methods for more on how to construct validation schemas.

By default, unknown environment variables (environment variables whose keys are not present in the schema) are allowed and do not trigger a validation exception. By default, all validation errors are reported. You can alter these behaviors by passing an options object via the validationOptions key of the forRoot() options object. This options object can contain any of the standard validation options properties provided by Joi validation options. For example, to reverse the two settings above, pass options like this:

app.module.ts

  1. import * as Joi from '@hapi/joi';
  2. @Module({
  3. imports: [
  4. ConfigModule.forRoot({
  5. validationSchema: Joi.object({
  6. NODE_ENV: Joi.string()
  7. .valid('development', 'production', 'test', 'provision')
  8. .default('development'),
  9. PORT: Joi.number().default(3000),
  10. }),
  11. validationOptions: {
  12. allowUnknown: false,
  13. abortEarly: true,
  14. },
  15. }),
  16. ],
  17. })
  18. export class AppModule {}

The @nestjs/config package uses default settings of:

  • allowUnknown: controls whether or not to allow unknown keys in the environment variables. Default is true
  • abortEarly: if true, stops validation on the first error; if false, returns all errors. Defaults to false.

Note that once you decide to pass a validationOptions object, any settings you do not explicitly pass will default to Joi standard defaults (not the @nestjs/config defaults). For example, if you leave allowUnknowns unspecified in your custom validationOptions object, it will have the Joi default value of false. Hence, it is probably safest to specify both of these settings in your custom object.

Hoodies, T-shirts, and accessories!

Support our future development by shopping in the official store!

See more

Custom getter functions

ConfigService defines a generic get() method to retrieve a configuration value by key. We may also add getter functions to enable a little more natural coding style:

  1. @Injectable()
  2. export class ApiConfigService {
  3. constructor(private configService: ConfigService) {}
  4. get isAuthEnabled(): boolean {
  5. return this.configService.get('AUTH_ENABLED') === 'true';
  6. }
  7. }
  1. @Dependencies(ConfigService)
  2. @Injectable()
  3. export class ApiConfigService {
  4. constructor(configService) {
  5. this.configService = configService;
  6. }
  7. get isAuthEnabled() {
  8. return this.configService.get('AUTH_ENABLED') === 'true';
  9. }
  10. }

Now we can use the getter function as follows:

app.service.ts

  1. @Injectable()
  2. export class AppService {
  3. constructor(apiConfigService: ApiConfigService) {
  4. if (apiConfigService.isAuthEnabled) {
  5. // Authentication is enabled
  6. }
  7. }
  8. }
  1. @Dependencies(ApiConfigService)
  2. @Injectable()
  3. export class AppService {
  4. constructor(apiConfigService) {
  5. if (apiConfigService.isAuthEnabled) {
  6. // Authentication is enabled
  7. }
  8. }
  9. }

Expandable variables

The @nestjs/config package supports environment variable expansion. With this technique, you can create nested environment variables, where one variable is referred to within the definition of another. For example:

  1. APP_URL=mywebsite.com
  2. SUPPORT_EMAIL=support@${APP_URL}

With this construction, the variable SUPPORT_EMAIL resolves to 'support@mywebsite.com'. Note the use of the ${...} syntax to trigger resolving the value of the variable APP_URL inside the definition of SUPPORT_EMAIL.

Hint For this feature, @nestjs/config package internally uses dotenv-expand.

Enable environment variable expansion using the expandVariables property in the options object passed to the forRoot() method of the ConfigModule, as shown below:

app.module.ts

  1. @Module({
  2. imports: [
  3. ConfigModule.forRoot({
  4. // ...
  5. expandVariables: true,
  6. }),
  7. ],
  8. })
  9. export class AppModule {}

Using in the main.ts

While our config is a stored in a service, it can still be used in the main.ts file. This way, you can use it to store variables such as the application port or the CORS host.

To access it, you must use the app.get() method, followed by the service reference:

  1. const configService = app.get(ConfigService);

You can then use it as usual, by calling the get method with the configuration key:

  1. const port = configService.get('PORT');