Very simple Env get function for NodeJS

Usage no npm install needed!

<script type="module">
  import secjsEnv from 'https://cdn.skypack.dev/@secjs/env';


Env 🔁

Very simple Env get function for NodeJS

GitHub language count Repository size License

The intention behind this repository is to always maintain a viable and simple Env function to use in any type of NodeJS Framework. @secjs/env uses dotenv library to find .env file in the project root, you can run your application with environment NODE_ENV set and @secjs/env will try to find the .env.${NODE_ENV}.


NODE_ENV=testing node index.js

If index.js file is using Env function the singleton of environment will run and find verify is exists NODE_ENV. In this case @secjs/env will use .env.testing file.


You can install the Env function using;

yarn add @secjs/env


You can call the Env function to get an env variable

import Env from '@secjs/env'

// Simulating .env file

// The response value will be the value of DB_DATABASE variable or my-database by default
const db = Env('DB_DATABASE', 'my-database')
console.log(db) // 'database'

const dbPort = Env('DB_PORT', '5432')
console.log(dbPort) // '5432'

const dbDebug = Env('DB_DEBUG', 'false')
console.log(dbDebug) // 'false' // Same as true value

const dbPortCasted = Env({ name: 'DB_PORT', type: 'number' }, 5432)
console.log(dbPortCasted) // 5432

const dbDebugCasted = Env({ name: 'DB_DEBUG', type: 'boolean' }, false)
console.log(dbDebugCasted) // false