Use envalid instead. Example:

Usage no npm install needed!

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


🌹 @sharyn/env


Use envalid instead. Example:

const envalid = require('envalid')
const pick = require('lodash.pick')
const either = require('@sharyn/util.either')
const swit = require('@sharyn/util.swit')

const { email, bool, port, str } = envalid

const varDefs = {
  STAGE: str({ choices: ['dev', 'local-prod', 'staging', 'prod'] }),
  TRUE: bool(),
  EMAIL: email({ desc: 'The email of the admin' }),
  PORT: port(),

const env = envalid.cleanEnv(
    STAGE: varDefs.STAGE,
      ['dev', 'local-prod', pick(varDefs, 'TRUE', 'PORT')],
      ['staging', 'prod', pick(varDefs, 'EMAIL', 'PORT')]
  { strict: true }

module.exports = env


npm install --save-dev @sharyn/env
# or
yarn add --dev @sharyn/env


With @sharyn/env, you can import your environment variables directly:

import { NODE_ENV, PORT, DATABASE_URL } from '@sharyn/env'

This package simply runs dotenv/config and exports process.env for a convenient access.

Just use @sharyn/env in every file that needs access to the environment, and forget about dotenv and process.env completely.


@sharyn/env.check can also help you validate environment variables.

This package is part of Sharyn, a collection of utilities and helpers.