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

nestjs-env

v2.1.2

Published

Using this library you can easily work with environment variables

Downloads

32

Readme

Install

Yarn

yarn add nestjs-env

NPM

npm install nestjs-env --save

This library is not responsible for loading environment variables from a file, for this you need to use env-cmd or dotenv.

Getting Started

Let's imagine that we have a folder called src/config in our project that contains several configuration files.

/src
├── app.module.ts
├── app.service.ts
├── config
│   ├── app.config.ts
│   └── index.ts
bootstrap.ts

Example app.config.ts

import { Env } from 'nestjs-env';

export class AppConfig {
  @Env('PORT', { default: 3000 })
  port: number;

  @Env('NODE_ENV')
  env: string;

  get isDevelopment() {
    return this.env === 'development';
  }
}

Let's register the config module in app.module.ts

import { Module } from '@nestjs/common';
import { EnvModule } from 'nestjs-env';
import { AppConfig } from 'src/config';

@Module({
  imports: [EnvModule.register([AppConfig])],
})
export class AppModule {}

Usage

Now we are ready to inject our AppConfig anywhere we'd like.

import { AppConfig } from 'src/config';

@Injectable()
class AppService {
  constructor(private readonly appConfig: AppConfig) {
    console.log(this.appConfig.isDevelopment);
  }
}
import { AppConfig } from 'src/config';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const config = app.get(AppConfig);

  await app.listen(config.port);
}

That's it!

CLI

The nestjs-env CLI is a command-line interface tool that helps you to build env example file.

$ nestjs-env generate

| Options | Alias | Description | Default | |-------------|-------|--------------------------------------------------------------|--------------| | --filename | -f | Name of the file to which the example will be written. | .env.example | | --pattern | -p | Template string specifying the names of files with configs | .config.ts | | --ignore | -i | Specify directory that should be excluded | node_modules | | --directory | -d | Specifies the base directory from which file scanning begins | src/ | | --output | -o | Specify an output folder for generated file | | | --print | | Prints an output to the console | false |


License

The MIT License (MIT)