1.2.0 • Published 4 months ago

@daopk/env v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

@daopk/env

npm-image

This package provides a convenient way to enable type-safety for environment variables in your Node.js application by wrapping the functionality of dotenv and envalid libraries.

Installation

You can install this package using npm:

npm install @daopk/env

Usage

To use this package, create a file named env.ts in your source root folder and define your environment variables with their expected types using defineEnv function. For example:

import { defineEnv, num, str } from '@daopk/env';

export const env = defineEnv({
    NODE_ENV: str(),

    SERVER_HOST: str({ default: 'localhost' }),
    SERVER_PORT: num({ default: 3000 }),
});

After defining your environment variables, you can import the exported env variable from the env.ts file.

import { env } from '~/env';

console.log(env.NODE_ENV); // Prints the value of NODE_ENV as a string
console.log(env.SERVER_HOST); // Prints the value of SERVER_HOST as a string
console.log(env.SERVER_PORT); // Prints the value of SERVER_PORT as a number

To use the ~ alias in your TypeScript project, you need to configure the paths option in the tsconfig.json file. For example:

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "~/*": ["./src/*"]
    }
  }
}
1.2.0

4 months ago

1.1.3

8 months ago

1.1.1

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.0

2 years ago