env-config-check
v1.0.2
Published
A simple utility to ensure all required environment variables are set.
Downloads
6
Maintainers
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:
Load Environment Variables: If you're using a
.env
file to manage environment variables, load them at the start of your application using thedotenv
package (optional but recommended):require("dotenv").config();
Import and Use
env-config-check
: Import theenv-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!