npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nestjsplus/config

v1.0.14

Published

Flexible, Docker-friendly, Dotenv-based Configuration Module for NestJs

Downloads

3,776

Readme

Top Features

  • Docker-friendly: e.g., use .env files in dev/test, environment variables in production, or any combination
  • @hapi/joi-based validation of environment variables
  • Completely dynamic and customizable determination of the name/location of your .env file
    • means: no code changes to handle unique configs per environment
  • Default values (i.e., optional environment variables with default values)
  • Trace how an environment variable was resolved (i.e., came from external environment, .env file or as a default value) to help debug tricky problems between dev, test, production

Full Feature List

Documentation

Quick Start - Read This First

You can read more about how NestJSConfigManager works if you want. And the simple API is documented here. But this section should get you started quickly.

To install with npm (or run yarn equivalent):

npm install @nestjsplus/config

The package has one global Nest module (ConfigManagerModule), and one main class (ConfigManager) that you'll need to work with. The main idea is to create your own ConfigService (in the examples, we'll call it ConfigService, but you can call it whatever you want). You probably want this in its own module (in the examples, we'll call it ConfigModule), which you probably want to be global. You'll then provide your ConfigService for use in the rest of your application. This approach affords you a great deal of flexibility:

  • Centralized setup of the ConfigurationModule/Service
  • Use Dependency Injection to provide the service wherever needed
  • Easily override/mock the service for testing
  • Future-proof: if you later want to switch to another 3rd party config module, your dependencies are isolated in one place

Following these conventions, your ConfigModule might look like this:

// src/config/config.module.ts
import { Module, Global } from '@nestjs/common';
import { ConfigManagerModule } from '@nestjsplus/config';
import { ConfigService } from './config.service';

@Global()
@Module({
  imports: [
    ConfigManagerModule.register({
      useFile: 'config/development.env',
    }),
  ],
  providers: [ConfigService],
  exports: [ConfigService],
})
export class ConfigModule {}

This imports and registers the ConfigManagerModule. The register() method is how you configure the module. In this example, we explicitly provide a full path to the .env file via the useFile configuration option. This is simple, but not terribly flexible. We'll explore more flexible options below. When using a static file path with useFile, the path is relative to the root directory for the project, or the root directory in which the app is running (in test and production environments). For example, if the app currently has a structure like:

myproject
├── src
│   └── module1
├── dist
├── node_modules
├── test
├── config
|   └── development.env
└── package.json

The above useFile configuration method would result in the ConfigManagerModule looking for a dotenv-formatted file in:

myproject/config/development.env

Your ConfigService might look like this:

// src/config/config.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigManager } from '@nestjsplus/config';
import * as Joi from '@hapi/joi';

@Injectable()
export class ConfigService extends ConfigManager {
  // Our custom "schema"
  // We supply it to the ConfigManager by extending the
  // ConfigManager class and implementing the
  // provideConfigSpec() method, which simply returns
  // our custom schema
  provideConfigSpec() {
    return {
      DB_HOST: {
        validate: Joi.string(),
        required: false,
        default: 'localhost',
      },
      DB_PORT: {
        validate: Joi.number()
          .min(5000)
          .max(65535),
        required: false,
        default: 5432,
      },
      DB_USERNAME: {
        validate: Joi.string(),
        required: true,
      },
      DB_PASSWORD: {
        validate: Joi.string(),
        required: true,
      },
      DB_NAME: {
        validate: Joi.string(),
        required: true,
      },
    };
  }
}

Your ConfigService (you can choose any name you want) is a derived class that extends the ConfigManager class provided by the package. You must implement the provideConfigSpec() method. This is where you define your schema. Read more about schemas here.

With this in place, you can use your ConfigService anywhere in your project. For example, assuming a .env file like:

// myproject/config/development.env
DB_USERNAME=john
DB_PASSWORD=mypassword
DB_NAME=mydb
.
.
.

A service could then use it like this:

// src/app.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from './config/config.service';

@Injectable()
export class AppService {
  private userName: string;

  constructor(configService: ConfigService) {
    this.userName = configService.get<string>('DB_USERNAME'));
  }

  getHello(): string {
    return `Hello ${this.userName}`;
  }
}

And calling getHello() would return

Hello john when run with this configuration.

Dynamic env file location example

Let's say you have two environments: development and test, and want to set up your ConfigService to dynamically locate the .env file that is appropriate to each environment. Let's assume that development uses one set of database credentials, and test uses another.

This would be well represented by having two .env files. Perhaps they're stored in a folder like myproject/config (this is just an example; they can be stored wherever you want). The files might look like:

For development:

// myproject/config/development.env
DB_USERNAME=devdbuser
DB_PASSWORD=devdbpass
DB_NAME=devdb

For test:

// myproject/config/test.env
DB_USERNAME=testdbuser
DB_PASSWORD=testdbpass
DB_NAME=testdb

How can we accommodate this dynamic file location without modifying our code? The useFile() method of configuration shown above won't work for this. You need a way to read a different .env file for each environment. A typical approach is to use a specific environment variable (typically NODE_ENV, though you can choose whatever you want) to indicate what the active environment is. For example, when running in development, you'd have NODE_ENV equal to 'development', and in test, NODE_ENV's value would equal 'test'.

Based on this, you can use the useEnv method of configuring the ConfigManagerModule. The useEnv method is described in the Module Configuration Options section, but a simple example is shown below.

To accommodate this requirement, we'd modify the way we register the ConfigManagerModule as follows, replacing useFile with useEnv:

// src/config/config.module.ts
import { Module, Global } from '@nestjs/common';
import { ConfigManagerModule } from '@nestjsplus/config';
import { ConfigService } from './config.service';

@Global()
@Module({
  imports: [
    ConfigManagerModule.register({
      useEnv: {
        folder: 'config',
      },
    }),
  ],
  providers: [ConfigService],
  exports: [ConfigService],
})
export class ConfigModule {}

If instead of NODE_ENV we wanted to use an environment variable like MY_ENVIRONMENT to signify which environment we're running (e.g., MY_ENVIRONMENT is equal to 'development' when we're in our development environment), we'd identify that environment variable using the option envKey, as shown below:

// src/config/config.module.ts
import { Module, Global } from '@nestjs/common';
import { ConfigManagerModule } from '@nestjsplus/config';
import { ConfigService } from './config.service';

@Global()
@Module({
  imports: [
    ConfigManagerModule.register({
      envKey: 'MY_ENVIRONMENT',
      useEnv: {
        folder: 'config',
      },
    }),
  ],
  providers: [ConfigService],
  exports: [ConfigService],
})
export class ConfigModule {}

Completely custom env file location

The useEnv method provides significant flexibility, but more complex structures require an even more flexible approach. To handle arbitrarily complex environments, a third method, useFunction, is available to write custom JavaScript code to generate the appropriate path and filename dynamically. This is covered in Using a custom function.

What's next?

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Author

  • John Biundo (Y Prospect on Discord)

License

Licensed under the MIT License - see the LICENSE file for details.