Skip to content

Commit 7b1c939

Browse files
docs: document new config module attributes, update migration guide
1 parent 5bc5c4b commit 7b1c939

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

content/migration.md

+14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ CacheModule.registerAsync({
146146

147147
Where `KeyvRedis` is imported from the `@keyv/redis` package. See the [Caching documentation](/techniques/caching) to learn more.
148148

149+
#### Config module
150+
151+
If you're using the `ConfigModule` from the `@nestjs/config` package, be aware of several breaking changes introduced in `@nestjs/[email protected]`. Most notably, the order in which configuration variables are read by the `ConfigService#get` method has been updated. The new order is:
152+
153+
- Internal configuration (config namespaces and custom config files)
154+
- Validated environment variables (if validation is enabled and a schema is provided)
155+
- The `process.env` object
156+
157+
Previously, validated environment variables and the `process.env` object were read first, preventing them from being overridden by internal configuration. With this update, internal configuration will now always take precedence over environment variables.
158+
159+
Additionally, the `ignoreEnvVars` configuration option, which previously allowed disabling validation of the `process.env` object, has been deprecated. Instead, use the `validatePredefined` option (set to `false` to disable validation of predefined environment variables). Predefined environment variables refer to `process.env` variables that were set before the module was imported. For example, if you start your application with `PORT=3000 node main.js`, the `PORT` variable is considered predefined. However, variables loaded by the `ConfigModule` from a `.env` file are not classified as predefined.
160+
161+
A new `skipProcessEnv` option has also been introduced. This option allows you to prevent the `ConfigService#get` method from accessing the `process.env` object entirely, which can be helpful when you want to restrict the service from reading environment variables directly.
162+
149163
#### Node.js v16 no longer supported
150164

151165
Starting with NestJS 11, Node.js v16 is no longer supported, as it reached its end-of-life (EOL) on September 11, 2023. NestJS 11 now requires **Node.js v20 or higher**.

content/techniques/configuration.md

+4
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ constructor(private configService: ConfigService<{ PORT: number }, true>) {
283283
}
284284
```
285285

286+
> info **Hint** To make sure the `ConfigService#get` method retrieves values exclusively from custom configuration files and ignores `process.env` variables, set the `skipProcessEnv` option to `true` in the options object of the `ConfigModule`'s `forRoot()` method.
287+
286288
#### Configuration namespaces
287289

288290
The `ConfigModule` allows you to define and load multiple custom configuration files, as shown in <a href="techniques/configuration#custom-configuration-files">Custom configuration files</a> 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:
@@ -453,6 +455,8 @@ The `@nestjs/config` package uses default settings of:
453455

454456
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.
455457

458+
> info **Hint** To disable validation of predefined environment variables, set the `validatePredefined` attribute to `false` in the `forRoot()` method's options object. Predefined environment variables are process variables (`process.env` variables) that were set before the module was imported. For example, if you start your application with `PORT=3000 node main.js`, then `PORT` is a predefined environment variable.
459+
456460
#### Custom validate function
457461

458462
Alternatively, you can specify a **synchronous** `validate` function that takes an object containing the environment variables (from env file and process) and returns an object containing validated environment variables so that you can convert/mutate them if needed. If the function throws an error, it will prevent the application from bootstrapping.

0 commit comments

Comments
 (0)