SmartEnvSwitcher
SmartEnvSwitcher is a lightweight Node.js library for managing environment variables across multiple environments, such as development, staging, and production. It simplifies the process of switching between environments and updating .env files programmatically.
Features
- Load and switch between multiple environment files.
- Add, update, or retrieve environment variables dynamically.
- Automatically save changes to
.env files.
- List available environments in the project.
Installation
Install SmartEnvSwitcher via npm:
npm install smart-env-switcher
Usage
1. Setup Environment Files
Create a folder to store your environment files (e.g., ./envs) and add .env files for each environment (e.g., development.env, production.env).
Example development.env:
API_KEY=123456
DB_HOST=localhost
2. Import and Initialize SmartEnvSwitcher
const SmartEnvSwitcher = require('smart-env-switcher').default;
const envSwitcher = new SmartEnvSwitcher('./envs', 'development');
3. Get Environment Variables
Retrieve a variable from the currently loaded environment:
console.log(envSwitcher.get('API_KEY'));
4. Set or Update Environment Variables
Add or update a variable in the current environment:
envSwitcher.set('NEW_KEY', 'new_value');
console.log(envSwitcher.get('NEW_KEY'));
5. Switch Environments
Switch to a different environment:
envSwitcher.switchEnv('production');
console.log(envSwitcher.get('API_KEY'));
6. List all loaded environment variables
Get a list of all environment variables in the currently loaded .env file:
console.log(envSwitcher.list());
7. List All Available Environments
Get a list of all .env files:
console.log(envSwitcher.listEnvironments());
8. Clear Environment Variables
Clear all loaded environment variables and save the changes to the .env file:
envSwitcher.clearEnv();
console.log(envSwitcher.list());
9. Remove an Environment Variable
Remove a specific environment variable and save the changes:
envSwitcher.remove('API_KEY');
console.log(envSwitcher.get('API_KEY'));
API Reference
new SmartEnvSwitcher(envDir, defaultEnv)
envDir (string): Path to the directory containing .env files.
defaultEnv (string): The default environment to load.
envSwitcher.get(key)
key (string): The environment variable name to retrieve.
- Returns: The value of the variable or
null if not found.
envSwitcher.set(key, value)
key (string): The environment variable name to set.
value (string): The value to assign to the variable.
envSwitcher.switchEnv(envName)
envName (string): The environment name (file name without .env) to switch to.
envSwitcher.list()
- Returns: A record of all loaded environment variables as key-value pairs.
envSwitcher.listEnvironments()
- Returns: An array of available environment names.
envSwitcher.clearEnv()
- Clears all environment variables and saves the changes to the .env file.
envSwitcher.listEnvironments()
- Removes the environment variable from the current environment and saves the changes.
Example Project Structure
project-directory/
├── envs/
│ ├── development.env
│ ├── production.env
├── index.js
Example Code
const SmartEnvSwitcher = require('smart-env-switcher');
const envSwitcher = new SmartEnvSwitcher('./envs', 'development');
console.log(envSwitcher.get('API_KEY'));
envSwitcher.set('NEW_VARIABLE', 'value');
envSwitcher.switchEnv('production');
console.log(envSwitcher.get('API_KEY'));
console.log(envSwitcher.list());
console.log(envSwitcher.listEnvironments());
envSwitcher.clearEnv();
console.log(envSwitcher.list());
envSwitcher.remove('API_KEY');
console.log(envSwitcher.get('API_KEY'));
Contributing
Contributions are welcome! Feel free to submit issues or pull requests on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.