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 πŸ™

Β© 2026 – Pkg Stats / Ryan Hefner

@efstajas/validate-env

v0.3.3

Published

πŸš“πŸ“‹βš οΈ A simple node.js utility that type-checks your process.env variables based on a .env.template file.

Readme

πŸš“ validate-env

Node.js 10.x and 12.x CI

A small utility for checking process.env based on a .env.template file in node.js. You can use it to make sure all your env variables are set before running your application, and optionally ensure they're of the right types too.

⬇️ Install

Simply install with npm or yarn. validate-env is available on the NPM registry and on GitHub Packages.

npm install @efstajas/validate-env

or

yarn add @efstajas/validate-env

🎬 Getting started

First, create your .env.template file. This file contains all the .env variables you want to validate for. While you should never commit a .env file, committing the .env.template makes a lot of sense β€” everyone working on your project can see immediately what values they need, and validation will work as expected on any client. An example .env.template might look something like this:

FOO=string
BAR=number
FOOBAR=array
FOOBARFOO=boolean?

This .env.template means you expect all the variables FOO, BAR, and FOOBAR to exist. The ? at the end of the FOOBARFOO variable's type means that it's an optional value β€” so, validation won't fail if it's not set, but if it is, it must be of type boolean. As you can see, after the =, you can specify a type for that given variable:

  • number means your variable must be numeric, meaning it can be something like 1 or 004 or even 6e+2.
  • array means your variable must be a valid JSON array, like ["foo", 123]. Please note it must be valid JSON, meaning strings are to be double-quoted.
  • boolean means your variable must be either 'true' or 'false'
  • string means your variable must be a valid string. In practice, any value will pass this test, because .env variables are always strings.

πŸ’‘You can put comments in your env template by using #. Great for annotations or sharing default values!

Usage

To run the test, simply import the main function, and pass it your .env.template file path. It returns a Promise that will resolve to a ValidatorResult.

import validateEnv from '@efstajas/validate-env'

validateEnv('./path/to/your/.env.template').then((r) => {
  if (r.result === 'pass') {
    // Your .env is valid!
  }

  if (r.result === 'fail') {
    // Something is wrong in your .env
  }
}).catch((e) => {
  /*
  Something went wrong while validating β€”
  maybe we couldn't open the file, or the
  template itself is invalid.
  */
  console.error(e)
})

The ValidatorResult contains either a SuccessPayload like { result: 'pass' }, or a FailedPayload which includes more info about what exactly failed:

validateEnv('./path/to/your/.env.template').then((r) => {
  if (r.result === 'fail') {
    const { failedVar } = r

    const {
      name,
      expectedType
    } = failedVar

    switch (failedVar.reason) {
      case 'MISSING':
        // Variable is missing from .env
      case 'WRONG_TYPE':
        // Variable is present, but doesn't match expected type
    }
  }
})

Normal usage: Validating .env before starting your app

Usually, you would want to validate your .env at the very beginning of your app, and if it fails, don't even initialize it. The best way to achieve this is to just wrap your initialization routine into a function, and then call it only if the validateEnv result indicates that your .env is valid. For an express application, it could look something like this:

// Use dotenv to load .env file into process.env
import * as dotenv from 'dotenv'
dotenv.config()

import express from 'express'
import validateEnv from '@efstajas/validate-env'

const initializeApplication = () => {
  const server = express.server

  // Register routes, middleware etc.

  server.listen(8000)
}

validateEnv('./.env.template').then((r) => {
  if (r.result === 'pass') {
    initializeApplication()
  }
})

Silent mode

By default, validate-env prints warnings or a success message to the console automatically after validation. If you want to handle logs by yourself, you can disable this behavior by passing the silent option:

validateEnv('./.env.template', { silent: true }).then((r) => {
  if (r.result === 'fail') {
    const { failedVar } = r

    const {
      name,
      expectedType
    } = failedVar

    switch (failedVar.reason) {
      case 'MISSING':
        console.log(`Variable ${name} is missing in .env. Expected type: ${expectedType}`)
        break
      case 'WRONG_TYPE':
        console.log(`Variable ${name} isn't of expected type ${expectedType}.`)
        break
    }

    return
  }

  console.log('.env is valid πŸŽ‰')
  initializeApplication()
  }
})