Socket
Socket
Sign inDemoInstall

nestjs-dotenv

Package Overview
Dependencies
26
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nestjs-dotenv

NestJS .env config Module


Version published
Weekly downloads
136
decreased by-28.42%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

NestJS .env config Module

alt cover

More NestJS libs on alariblog.ru

npm version npm version npm version npm version

NestJS .env package allows you easily use .env file to store your app configs.

npm i nestjs-dotenv

Then register module in your root app.module

import { ConfigModule } from 'nestjs-dotenv';

@Module({
	imports: [
		// ...
		ConfigModule.forRoot(),
	],
})
export class AppModule {}

It will load .env file from your project root. If you need to change it's path just pass it to forRoot method like this:

ConfigModule.forRoot(myPath);

To use ConfigService in any service or controller just inject it with @ConfigInjection decorator in the constructor:

import { ConfigService } from 'nestjs-dotenv';

constructor(
	private readonly configService: ConfigService
) {}

Get env value

To get a value from .env file just call get() method:

this.configService.get('JIRA_TOKEN');
  • JIRA_TOKEN - name of your key in .env file:
JIRA_TOKEN=0000000000000

Method returns string.

Get env value and convert it to specific type

Instead of get() method use getWithType():

this.configService.getWithType('JIRA_TOKEN', 'string');

getWithType() get 3 parameters:

  • name of your key in .env file.
  • type of your data in .env file.
  • (optional) Enum to convert data to, if your type is 'enum'.

Available types:

// 'number'
VALUE=1

// 'string'
VALUE=mystring

// 'boolean'
VALUE=true

// 'array'
VALUE=[1,2,3]

// 'object'
VALUE={"key": "value"}

/** 'enum'
 enum Color {
    White = 1
    Black = 2
} **/
VALUE=White

Reloading file

To reload env dynamically use reload() method:

this.configService.reload();

Keywords

FAQs

Last updated on 28 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc