Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

std-env

unjs27.6mMIT3.8.0TypeScript support: included

Runtime agnostic JS utils

readme

std-env

npm npm bundlephobia

Runtime agnostic JS utils

Installation

# Using npm
npm i std-env

# Using pnpm
pnpm i std-env

# Using yarn
yarn add std-env

Usage

// ESM
import { env, isDevelopment, isProduction } from "std-env";

// CommonJS
const { env, isDevelopment, isProduction } = require("std-env");

Flags

  • hasTTY
  • hasWindow
  • isDebug
  • isDevelopment
  • isLinux
  • isMacOS
  • isMinimal
  • isProduction
  • isTest
  • isWindows
  • platform
  • isColorSupported
  • nodeVersion
  • nodeMajorVersion

You can read more about how each flag works from ./src/flags.ts.

Provider Detection

std-env can automatically detect the current runtime provider based on environment variables.

You can use isCI and platform exports to detect it:

import { isCI, provider, providerInfo } from "std-env";

console.log({
  isCI, // true
  provider, // "github_actions"
  providerInfo, // { name: "github_actions", isCI: true }
});

List of well known providers can be found from ./src/providers.ts.

Runtime Detection

std-env can automatically detect the current JavaScript runtime based on global variables, following the WinterCG Runtime Keys proposal:

import { runtime, runtimeInfo } from "std-env";

// "" | "node" | "deno" | "bun" | "workerd" ...
console.log(runtime);

// { name: "node" }
console.log(runtimeInfo);

You can also use individual named exports for each runtime detection:

[!NOTE] When running code in Bun and Deno with Node.js compatibility mode, isNode flag will be also true, indicating running in a Node.js compatible runtime.

Use runtime === "node" if you need strict check for Node.js runtime.

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

List of well known providers can be found from ./src/runtimes.ts.

Platform-Agnostic env

std-env provides a lightweight proxy to access environment variables in a platform agnostic way.

import { env } from "std-env";

Platform-Agnostic process

std-env provides a lightweight proxy to access process object in a platform agnostic way.

import { process } from "std-env";

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v3.8.0

compare changes

🚀 Enhancements

  • provider: Add deno-deploy (#117)
  • providers: Detect FIREBASE_APP_HOSTING ci (#146)

📦 Build

🏡 Chore

❤️ Contributors

v3.7.0

compare changes

🚀 Enhancements

  • provider: Add railway support (#106)

🩹 Fixes

  • Check bun runtime before node (#107)

💅 Refactors

  • Clarify runtimes and isNode behavior (#108)

🏡 Chore

❤️ Contributors

v3.6.0

compare changes

🚀 Enhancements

  • Add zeabur provider (#93)
  • providers: Add codesphere detection (#95)

🩹 Fixes

  • isColorSupported: Inverted condition for TERM=dumb (#97)

🌊 Types

  • Type runtime export (#92)

🏡 Chore

❤️ Contributors

v3.5.0

compare changes

🚀 Enhancements

  • Add aws_amplify provider (#91)

🏡 Chore

❤️ Contributors

v3.4.3

compare changes

🩹 Fixes

  • Conditional check for process.versions?.bun (#75)

🏡 Chore

  • Update build dependencies (cd6cbf7)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.4.2

compare changes

📦 Build

  • es6 compatibiliy for commonjs bundle (#74)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.4.1

compare changes

🩹 Fixes

  • Access process from globalThis (#73)

❤️ Contributors

  • Patrick Pircher

v3.4.0

compare changes

🚀 Enhancements

  • Universal env shim (#61)
  • providers: Detect VERCEL and VERCEL_ENV (#58)
  • isColorSupported flag (b80677b)
  • Expose providerInfo (3c547d8)
  • Platform agnostic process (48b4fc0)
  • process: Support process.versions (1ad71f6)
  • env: Support import.meta.env and Deno.env (ceb04d5)
  • Export nodeVersion and nodeMajorVersion (#60)
  • Detect stackblitz using process.versions.webcontainer (#63)
  • Detect the underlying runtime (#66)

🔥 Performance

  • Reduce dependency on process proxy for flags (0d7b84f)
  • Decouple nodeENV from env proxy (89c0411)

🩹 Fixes

  • Add default fallback for platform flag (d71ce85)
  • Update bun runtime detection (#67)
  • Use globalThis.Bun for check (ca852ac)

💅 Refactors

  • Split flags (584e9f4)
  • Improve process and env types (581c674)
  • Cleanup provider detection (3091a5b)
  • provider: Remove dependency from process shim (c1c5a68)
  • Decouple flags from process shim (6b087e2)
  • Remove _process export (ce21499)

📦 Build

  • Minify dist by default (4e67307)

🏡 Chore

  • Update dev dependencies (c98a7ab)
  • Add autofix ci (d817123)
  • Fix prettierrc (b720e2b)
  • Improve docs (1d4ef96)
  • Add typecheck script (0694e19)
  • Fix internal types and expose EnvObject (058abc6)
  • Update title (25ebcfb)
  • Create playground (6d92124)
  • Add bun playground (0a576f2)
  • Update docs (19a7651)
  • Fix ci (4b0ae64)
  • Update unbuild to v2 (23cb666)
  • Update node playground to jiti (12efa68)
  • Split global types (ec4c8f7)

🎨 Styles

  • Format with prettier v3 (d198921)

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Tom Lienard (@QuiiBz)
  • Nozomu Ikuta

v3.3.3

compare changes

🩹 Fixes

  • pkg: Add types field to the exports (#51)
  • Support static process.env.NODE_ENV (#45)

🏡 Chore

  • Fix coverage test (92152a1)
  • Git ignore coverage dir (cd52a9e)
  • Lint (3832bcd)
  • Update dev dependencies (acd471d)

✅ Tests

  • Add defaults test (17009d9)
  • Expect any provider (a7d8c43)

❤️ Contributors

v3.3.2

🩹 Fixes

  • Detect isTest from NODE_ENV as well (#39)

🏡 Chore

  • Update dev dependencies (cdb65ab)
  • Use changelogen for release (62b8d85)

🎨 Styles

  • Format with prettier (0469d74)

❤️ Contributors

3.3.1 (2022-11-14)

3.3.0 (2022-10-15)

Features

3.2.1 (2022-08-12)

3.2.0 (2022-08-12)

Features

  • detect cloudflare_pages provider (#31) (411b3c0)

3.1.1 (2022-04-15)

Bug Fixes

3.1.0 (2022-04-15)

Features

3.0.1 (2021-11-05)

Bug Fixes

  • set ci: false for unkown envs (2d5a646)

3.0.0 (2021-11-03)

Bug Fixes

3.0.0-0 (2021-11-02)

⚠ BREAKING CHANGES

  • v3 rewrite (#15)

Features

2.3.1 (2021-09-29)