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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sharyn/env.check

v1.0.2

Published

Helpers to check the validity of your environment variables.

Downloads

5

Readme

🌹 @sharyn/env.check

Deprecated

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(
  process.env,
  {
    STAGE: varDefs.STAGE,
    ...swit(
      process.env.STAGE,
      ['dev', 'local-prod', pick(varDefs, 'TRUE', 'PORT')],
      ['staging', 'prod', pick(varDefs, 'EMAIL', 'PORT')]
    ),
  },
  { strict: true }
)

module.exports = env

@sharyn/env/check provides 3 methods to check that your process.env is loaded with the right variables.

Installation

npm i @sharyn/env.check
# or
yarn add @sharyn/env.check

You can alternatively install the @sharyn/browser package, or the entire sharyn library.

Usage

In the following example, the STAGE corresponds to the various deployment stages, which I like to name dev, local-prod (with NODE_ENV set to production), staging, and prod.

Create a file to check your env, env-check.js for instance:

import 'dotenv/config'
import { check, checkPresent, checkAbsent } from '@sharyn/env/check'

checkPresent('DATABASE_URL', 'REDIS_URL') // Checked for all stages

if (process.env.STAGE === 'dev') {
  checkPresent('WEBPACK_DEV_SERVER_PORT')
  checkAbsent('S3_BUCKET', 'ERROR_REPORTING')
}

if (process.env.STAGE === 'prod') {
  checkPresent('S3_BUCKET', 'ERROR_REPORTING')
  checkAbsent('WEBPACK_DEV_SERVER_PORT')
  check('API_URL', url => url.startsWith('https://'), name => `${name} is not an HTTPS URL.`)
}

You should check your environment variables at runtime, at the very beginning or your application. Before the initialization of your Express server for instance.

import './env-check'

Imports

Depending on the package you are using, you can import or require getFormFields in the following ways:

import { check, checkPresent, checkAbsent } from '@sharyn/env.check'
import { check, checkPresent, checkAbsent } from '@sharyn/env/check'
import { check, checkPresent, checkAbsent } from 'sharyn/env/check'