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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cjkihl/with-env

v1.0.14

Published

Load environment variables in a monorepo

Readme

@cjkihl/with-env

A utility for loading environment variables in monorepos and executing commands with the loaded environment.

Features

  • 🔧 Monorepo Support: Automatically finds and loads environment files from the monorepo root
  • 📁 Multiple Env Files: Supports loading from .env.default and .env.local files (configurable)
  • 🚀 Command Execution: Executes commands with the loaded environment variables
  • 🛡️ Production Safety: Optionally skips loading env files in production environments
  • 📦 CLI Tool: Includes a command-line interface for easy integration
  • 🔌 Programmatic API: Can be used as a library in your Node.js applications

Installation

npm install @cjkihl/with-env

Usage

Command Line Interface

The package provides a CLI tool that loads environment variables and executes commands:

# Basic usage - loads .env.default and .env.local (with .env.local overriding .env.default) and runs a command
npx with-env npm run dev

# With custom environment file
npx with-env --env-file .env.development npm run dev

# All arguments after the command are forwarded to the subprocess (no need to quote)
npx with-env bun run file.ts --limit=5

# Use -- to separate with-env options from the command when needed
npx with-env --env-file .env -- bun run file.ts --limit=5

Arguments (including flags like --limit=5) passed after the command are always forwarded to the subprocess, so you can extend scripts and use package.json scripts that append args.

Programmatic Usage

import { loadEnv } from '@cjkihl/with-env';

// Basic usage
await loadEnv({
  command: 'npm',
  args: ['run', 'dev']
});

// With custom configuration
await loadEnv({
  envFile: ['.env.development', '.env'],
  skipInProduction: false,
  inheritStdio: true,
  command: 'node',
  args: ['server.js']
});

Configuration

WithEnvConfig Interface

interface WithEnvConfig {
  envFile?: string[];           // Array of env file names to load in order (default: ['.env.default', '.env.local'])
  skipInProduction?: boolean;   // Skip loading env files in production (default: true)
  inheritStdio?: boolean;       // Inherit stdio from parent process (default: true)
  command?: string;             // Command to execute
  args?: string[];              // Arguments for the command
}

CLI Options

  • --env-file=<file>: Specify environment file(s) to load
  • --skip-production=<boolean>: Control whether to skip loading in production (default: true)
  • --inherit-stdio=<boolean>: Control stdio inheritance (default: true)

How It Works

  1. Monorepo Detection: Uses @manypkg/get-packages to find the monorepo root directory
  2. Environment Loading: Searches for environment files in the root directory in order of preference
  3. Variable Injection: Loads and parses the environment file using dotenv
  4. Process Execution: Spawns a child process with the loaded environment variables
  5. Error Handling: Provides detailed error messages and proper exit codes

Environment File Loading

The tool loads environment files in the specified order and merges them:

  1. Default behavior: Loads .env.default first, then .env.local (with .env.local overriding variables from .env.default)
  2. Custom order: Files are loaded in the order specified in the envFile array
  3. Merging: Later files override variables from earlier files
  4. Missing files: If a file doesn't exist, it's skipped without error

Examples

Development Workflow

# Load environment and start development server
npx with-env npm run dev

# Load specific environment file for testing
npx with-env --env-file=.env.test npm run test

Production Deployment

# Load environment and build for production
npx with-env --skip-production=false npm run build

# Deploy with custom environment
npx with-env --env-file=.env.production npm run deploy

Package.json scripts

You can wrap commands in with-env and still pass extra args from the runner. Everything after the command is processed in the subprocess.

{
  "scripts": {
    "run": "with-env bun run file.ts",
    "run2": "with-env bun run file.ts --limit=5"
  }
}
  • bun run run -- --limit=5 runs with-env bun run file.ts --limit=5; the subprocess receives --limit=5.
  • bun run run2 runs with args baked in. All args are processed in the subprocess.

Integration in Scripts

{
  "scripts": {
    "dev": "with-env npm run dev:server",
    "test": "with-env --env-file .env.test npm run test:runner"
  }
}

Error Handling

The tool provides comprehensive error handling:

  • File Not Found: Gracefully handles missing environment files
  • Parse Errors: Reports issues with malformed environment files
  • Command Failures: Returns detailed error information for failed commands
  • Process Errors: Handles spawn failures and provides context
  • Signal Handling: Properly forwards SIGINT, SIGTERM, and SIGHUP signals to child processes, ensuring proper cleanup when the parent process is terminated (e.g., via CTRL+C)

Requirements

  • Node.js >= 18.0.0
  • Works in monorepo environments (uses @manypkg/get-packages)

License

MIT