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 🙏

© 2025 – Pkg Stats / Ryan Hefner

smart-env-switcher

v1.0.6

Published

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 programm

Readme

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;

// Initialize with the folder containing .env files and the default environment
const envSwitcher = new SmartEnvSwitcher('./envs', 'development');

3. Get Environment Variables

Retrieve a variable from the currently loaded environment:

console.log(envSwitcher.get('API_KEY')); // Output: 123456

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')); // Output: new_value

5. Switch Environments

Switch to a different environment:

envSwitcher.switchEnv('production');
console.log(envSwitcher.get('API_KEY')); // Value from production.env

6. List all loaded environment variables

Get a list of all environment variables in the currently loaded .env file:

console.log(envSwitcher.list()); // Output: ['development', 'production']

7. List All Available Environments

Get a list of all .env files:

console.log(envSwitcher.listEnvironments()); // Output: ['development', 'production']

8. Clear Environment Variables

Clear all loaded environment variables and save the changes to the .env file:

envSwitcher.clearEnv();
console.log(envSwitcher.list()); // Output: {}

9. Remove an Environment Variable

Remove a specific environment variable and save the changes:

envSwitcher.remove('API_KEY');
console.log(envSwitcher.get('API_KEY')); // Output: null

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');

// Initialize the environment switcher
const envSwitcher = new SmartEnvSwitcher('./envs', 'development');

// Get a variable
console.log(envSwitcher.get('API_KEY'));

// Set a new variable
envSwitcher.set('NEW_VARIABLE', 'value');

// Switch to production
envSwitcher.switchEnv('production');
console.log(envSwitcher.get('API_KEY'));

// List all loaded variables in the current environment
console.log(envSwitcher.list()); // Output: { API_KEY: 'production_key', DB_HOST: 'production_host' }

// List all available environments
console.log(envSwitcher.listEnvironments());

// Clear all environment variables
envSwitcher.clearEnv();
console.log(envSwitcher.list()); // Output: {}

// Remove an environment variable
envSwitcher.remove('API_KEY');
console.log(envSwitcher.get('API_KEY')); // Output: null

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.