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

env-config-check

v1.0.2

Published

A simple utility to ensure all required environment variables are set.

Downloads

6

Readme

env-config-check

A simple utility to ensure all required environment variables are set before starting your Node.js application. With env-config-check, you can prevent common configuration-related issues that might arise when environment variables are missing, making your applications more reliable and easier to maintain.

Features

  • Easy to Use: Just define the required environment variables and call checkEnvVariables.
  • Customizable: Specify any number of required environment variables.
  • Lightweight: Minimal footprint, making it ideal for any Node.js application.
  • Error Handling: Provides clear, descriptive error messages when environment variables are missing.

Installation

You can easily install env-config-check via NPM. Run the following command in your project directory:

npm install env-config-check

If you're using Yarn, you can install it with:

yarn add env-config-check

Usage

Basic Usage

Using env-config-check is straightforward. Here’s how you can integrate it into your Node.js application:

  1. Load Environment Variables: If you're using a .env file to manage environment variables, load them at the start of your application using the dotenv package (optional but recommended):

    require("dotenv").config();
  2. Import and Use env-config-check: Import the env-config-check package and define the required environment variables that your application depends on.

    const checkEnvVariables = require("env-config-check");
    
    // Define the required environment variables
    const requiredEnvVars = ["DB_HOST", "DB_USER", "DB_PASS"];
    
    // Call the function to check if all required environment variables are set
    checkEnvVariables(requiredEnvVars);

    If any of the specified environment variables are missing, env-config-check will throw an error and prevent your application from starting, helping you catch configuration issues early.

Example

Below is a complete example demonstrating how to use env-config-check in a typical Node.js application:

// Load environment variables from .env file
require("dotenv").config();

// Import the env-config-check package
const checkEnvVariables = require("env-config-check");

// Define the required environment variables
const requiredEnvVars = ["DB_HOST", "DB_USER", "DB_PASS"];

// Check if all required environment variables are set
checkEnvVariables(requiredEnvVars);

// Start your application
console.log("All environment variables are set. Starting the application...");

Custom Error Handling

You can also use custom error handling by wrapping the checkEnvVariables call in a try-catch block:

try {
  const checkEnvVariables = require("env-config-check");
  const requiredEnvVars = ["DB_HOST", "DB_USER", "DB_PASS"];
  checkEnvVariables(requiredEnvVars);
} catch (error) {
  console.error("Environment configuration error:", error.message);
  process.exit(1); // Exit the application with an error code
}

This approach allows you to log errors or handle them in a way that’s more suitable for your application's needs.

Why Use env-config-check?

  • Prevents Startup Failures: Ensure that all necessary environment variables are set before your application starts, reducing the likelihood of runtime errors.
  • Improves Debugging: The package provides clear error messages, making it easier to identify and fix configuration issues.
  • Increases Application Reliability: By verifying environment configurations at startup, you can avoid common issues that could lead to downtime or unexpected behavior in production environments.

License

This package is licensed under the MIT License, meaning you’re free to use, modify, and distribute it, provided you include the original license. See the LICENSE file for more details.

Contributing

Contributions are welcome! If you have suggestions for new features, improvements, or bug fixes, feel free to open an issue or submit a pull request on the GitHub repository.

Author

Created and maintained by Dipak Ahirav.

Support

If you encounter any issues or have questions about using env-config-check, you can reach out by opening an issue on GitHub or contacting me directly.

Links

Changelog

All notable changes to this project will be documented in the CHANGELOG.md.


Try It Out

Ensure your Node.js application is correctly configured with all the necessary environment variables by installing env-config-check today:

npm install env-config-check

For more details, visit the NPM package page.


Conclusion

The env-config-check package is a simple yet powerful tool for validating environment variables before your Node.js application starts. By using this package, you can prevent configuration errors, improve the reliability of your applications, and make your development process smoother. Try it out today and make your environment setup foolproof!

Happy coding!