dotenvguard
v2.0.1
Published
A custom environment variable validator for Node.js applications.
Downloads
9
Maintainers
Readme
dotenvguard
Installation
npm
$ npm install dotenvguard
yarn
$ yarn add dotenvguard
pnpm
$ pnpm add dotenvguard
Usage
- Basic usage (warning mode only)
import { envguard } from 'dotenvguard';
/**
* @param key - The key to check in the environment variables.
*/
envguard('MY_TEST_KEY');
Warning: The key is missing or empty in .env file: MY_STRICT_KEY
- Advanced usage (error mode)
import { envguard } from 'dotenvguard';
/**
* @param key - The key to check in the environment variables.
* @param strict - Enabled strict mode
*/
envguard({ strict: ["MY_STRICT_KEY"] })
C:\XXX\XXX\XXX\XXX.js:41
throw new Error(errorMessage);
^
Error: Required key missing or empty in .env file: MY_STRICT_KEY
Utility
import { envinfo } from 'dotenvguard';
console.log(envinfo());
// { exists: boolean }
import { envinfo } from 'dotenvguard';
console.log(envinfo('advanced'));
// -- General information --
// - Current working directory: C:\XXX\XXX\XXX\XXX\dotenvguard
// - .env working directory: C:\XXX\XXX\XXX\XXX\dotenvguard
// -- ENV file information --
// - Created by: XXX
// - Created at: 2024-03-23T00:33:42.807Z
// - Updated at: 2024-03-23T00:33:42.807Z
// -- Logging time --
// - Logged time: 2024-03-23T00:37:14.207Z
// - User: XXX
// {
// exists: true,
// path: 'C:\\XXX\\XXX\\XXX\\XXX\\dotenvguard\\.env',
// createdAt: 2024-03-23T00:33:42.807Z,
// updatedAt: 2024-03-23T00:33:42.807Z
// }
Remember
- Throws error if .env has not been found (any mode)
C:\XXX\XXX\XXX\XXX.js:41
throw new Error(colors_1.default.red('.env file not found'));
^
Error: .env file not found