std-env

repository·main·Indexed 20 days ago

https://github.com/unjs/std-env

Runtime agnostic JavaScript utilities for detecting environments, runtimes, CI providers, and AI coding agents. It provides tools to identify JavaScript runtimes (Node.js, Bun, Deno, Workerd, etc.), CI/CD providers (GitHub Actions, Vercel, Netlify, etc.), and AI agents (Cursor, Claude, Devin, etc.), along with runtime-agnostic access to environment variables and platform flags.

Tokens
4.5K
Snippets
16
Records
24
Agent score
65%

What's inside std-env

  1. Detect the JavaScript runtime

    main

    Use std-env to identify the current execution environment (e.g., Node.js, Bun, Deno, or Workerd) following the WinterCG Runtime Keys proposal.

    Strict vs. Compatibility Checks:

    • Use runtime === 'node' for a strict check to ensure you are specifically in Node.js.
    • Use isNode if you want to include Bun or Deno when they are running in Node.js compatibility mode.

    Available named boolean exports include isNode, isBun, isDeno, isNetlify, isEdgeLight, isWorkerd, and isFastly.

    import { runtime, runtimeInfo } from "std-env";
    
    console.log(runtime); // "" | "node" | "deno" | "bun" | "workerd" ...
    console.log(runtimeInfo); // { name: "node" }
  2. Detect CI/CD providers

    main

    Identify if the code is running in a Continuous Integration (CI) environment and which provider is being used (e.g., GitHub Actions) by inspecting environment variables.

    Use detectProvider() if you need to re-run the detection logic manually.

    import { isCI, provider, providerInfo } from "std-env";
    
    console.log({ isCI, provider, providerInfo });
    // { isCI: true, provider: "github_actions", providerInfo: { name: "github_actions", ci: true } }
  3. Detect AI coding agents

    main

    Determine if the environment is being operated by an AI coding agent.

    Manual Override: You can explicitly specify the agent name by setting the AI_AGENT environment variable.

    Supported Agents: cursor, claude, devin, replit, gemini, codex, auggie, opencode, kiro, goose, pi, junie.

    Use detectAgent() to re-run detection.

    import { isAgent, agent, agentInfo } from "std-env";
    
    console.log({ isAgent, agent, agentInfo });
    // { isAgent: true, agent: "claude", agentInfo: { name: "claude" } }
  4. Reference: Environment flags

    main

    A list of boolean and metadata flags provided by std-env for environment branching.

    | Export             | Description                                          |
    | ------------------ | ---------------------------------------------------- |
    | `hasTTY`           | stdout TTY is available                             |
    | `hasWindow`        | Global `window` is available                         |
    | `isCI`             | Running in CI                                        |
    | `isColorSupported` | Terminal color output supported                      |
    | `isDebug`          | `DEBUG` env var is set                               |
    | `isDevelopment`    | `NODE_ENV` is `dev`/`development` or `MODE` is `development` |
    | `isLinux`          | Linux platform                                      |
    | `isMacOS`          | macOS (darwin) platform                             |
    | `isMinimal`        | `MINIMAL` env is set, CI, test, or no TTY            |
    | `isProduction`     | `NODE_ENV` or `MODE` is `production`                 |
    | `isTest`           | `NODE_ENV` is `test` or `TEST` env is set            |
    | `isWindows`        | Windows platform                                    |
    | `platform`         | Value of `process.platform`                          |
    | `nodeVersion`      | Node.js version string (e.g. `"22.0.0"`)            |
    | `nodeMajorVersion` | Node.js major version number (e.g. `22`)             |
  5. Reference: Universal environment access

    main

    Access environment variables and process-like objects in a runtime-agnostic way.

    | Export    | Description                                          |
    | --------- | ---------------------------------------------------- |
    | `env`     | Universal `process.env` (works across all runtimes)   |
    | `process` | Universal `process` shim (works across all runtimes) |
    | `nodeENV` | Current `NODE_ENV` value (undefined if unset)        |
  6. Use environment flags and platform info

    main

    Access boolean flags and platform metadata to conditionally execute code based on the environment, OS, or mode (development/production).

    import { env, isDevelopment, isProduction } from "std-env";
  7. Detect the CI/CD or Deployment provider

    main

    Use detectProvider() to identify the current CI/CD or deployment environment. It returns a ProviderInfo object containing the provider's name and metadata. Alternatively, you can import the provider constant for a quick string representation of the detected provider name.

    Supported providers include a wide range of services such as github_actions, gitlab, vercel, netlify, cloudflare_pages, deno-deploy, and many others. If no provider is detected, the name will be an empty string.

    import { detectProvider, provider } from "std-env";
    
    // Method 1: Get full info object
    const info = detectProvider();
    console.log(info.name); // e.g., "github_actions"
    
    // Method 2: Get just the name
    console.log(provider); // e.g., "github_actions"
  8. Detect and inspect providers

    main
    Use the provider detection API to identify the hosting or cloud provider (e.g., Vercel, AWS). You can check for a provider using provider, get detailed ProviderInfo, or use detectProvider to identify the ProviderName.
  9. Access the `process` global via `process`

    main

    The process constant provides a runtime-agnostic reference to the process global. If globalThis.process is available, it returns that object. If not, it returns a minimal shim containing only the env object. This is useful for maintaining compatibility with code that expects a process-like interface.

    import { process } from 'std-env';
    
    // Accessing env through the shimmed process object
    const debugMode = process.env?.DEBUG === 'true';
  10. Use runtime boolean flags for environment checks

    main

    For simple conditional logic, use the exported boolean flags to check if the code is running in a specific environment.

    Important Note on Node.js Compatibility: When running in Bun or Deno using Node.js compatibility modes, the isNode flag will be true. If you require a strict check specifically for the Node.js runtime, compare the runtime constant against the string "node" instead.

    import { isNode, isBun, isDeno, isFastly, isNetlify, isEdgeLight, isWorkerd, runtime } from "std-env";
    
    if (isBun) {
      // Bun-specific logic
    }
    
    // Strict Node.js check (avoids false positives from Bun/Deno compatibility modes)
    if (runtime === "node") {
      // Only runs in actual Node.js
    }
    
    if (isNode) {
      // Runs in Node.js OR Node-compatible runtimes (like Bun/Deno)
    }
  11. Get the current `nodeENV` value

    main

    The nodeENV constant provides the current value of the NODE_ENV environment variable. If NODE_ENV is not set in the environment, this value will be undefined. This is a convenient way to check for environments like development, production, or test without manual lookup.

    import { nodeENV } from 'std-env';
    
    if (nodeENV === 'production') {
      // perform production-only logic
    }
  12. Detect and inspect runtimes

    main

    Identify the JavaScript runtime executing your code.

    Runtime Checks:

    • isNode, isBun, isDeno
    • isEdgeLight, isFastly, isNetlify, isWorkerd

    Runtime Metadata:

    • runtime: Returns the current RuntimeName.
    • runtimeInfo: Returns detailed RuntimeInfo.
    • detectRuntime: (via runtime logic) to identify the environment.