Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@stone-js/env
Advanced tools
Fluent and simple API to deal with .env file and env variables for both browser and node.js.
Fluent and simple API to deal with .env file and env variables for both browser and node.js.
The @stone-js/env
library provides utility functions for managing environment variables in JavaScript and TypeScript applications. It helps developers retrieve, validate, and transform environment variables, supporting different data types such as strings, numbers, booleans, arrays, objects, and more. This ensures that the environment configuration is reliable and meets the expected requirements. This library is compatible with both vanilla JavaScript and TypeScript.
To install the Env
utility, you need to add it to your project. Assuming it’s part of a package you manage.
NPM:
npm i @stone-js/env
Yarn:
yarn add @stone-js/env
PNPM:
pnpm add @stone-js/env
The Env
module can only be imported via ESM import syntax:
import * as Env from '@stone-js/env';
To start using the @stone-js/env
library, you need to import the functions you want to use in your project. Below is a simple example of how to use the library to retrieve environment variables and ensure they meet the necessary validation requirements.
import { get, getString, getNumber, getBoolean } from '@stone-js/env';
// Retrieving an environment variable as a string
const apiUrl = getString('API_URL');
// Retrieving an environment variable as a number
const port = getNumber('PORT', { default: 3000 });
// Retrieving a boolean environment variable
const isProduction = getBoolean('IS_PRODUCTION', { default: false });
The @stone-js/env
library provides multiple functions to retrieve environment variables in different formats, allowing for strong validation and type safety. Below are the available functions and their descriptions:
The Options
interface provides configuration options for retrieving environment variables. Below are the properties available in the Options
interface:
type
('number' | 'boolean' | 'array' | 'object' | 'json' | 'enum' | 'email' | 'host' | 'url' | 'string'
): Specifies the expected type of the environment variable.format
('url' | 'host' | 'email'
): Specifies the format of the value if it is a string.enums
(string[]
): An array of possible values for enum validation.optional
(boolean
): Indicates if the environment variable is optional.default
(any
): The default value if the environment variable is not set.separator
(string
): Separator for parsing array or object values (default is ','
).tld
(boolean
): Indicates if a top-level domain is required for URLs or emails.protocol
(boolean
): Indicates if a protocol is required for URLs.version
(IPVersion
): Specifies the IP version if the type is 'host'
.get<T>(key: string, options?: Options | any): T
Retrieves the value of a specified environment variable.
key
(string): The environment variable key.options
(Options | any): Options for retrieving the value.T
.getString(key: string, options?: Options | string): string
Retrieves the specified environment variable value as a string.
key
(string): The environment variable key.options
(Options | string): Options for retrieving the value.getNumber(key: string, options?: Options): number
Retrieves the specified environment variable value as a number.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getBoolean(key: string, options?: Options): boolean
Retrieves the specified environment variable value as a boolean.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getArray(key: string, options?: Options): string[]
Retrieves the specified environment variable value as an array of strings.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getObject(key: string, options?: Options): Record<string, any>
Retrieves the specified environment variable value as an object.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getJson(key: string, options?: Options): unknown
Retrieves the specified environment variable value as a JSON object.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getEnum(key: string, enums: string[] | Options = [], defaultValue?: string, options?: Options): string
Retrieves the specified environment variable value as an enum.
key
(string): The environment variable key.enums
(string[] | Options): Array of possible enum values or options.defaultValue
(string, optional): Default value if not set.options
(Options): Options for retrieving the value.getEmail(key: string, options?: Options): string
Retrieves the specified environment variable value as an email address.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getUrl(key: string, options?: Options): string
Retrieves the specified environment variable value as a URL.
key
(string): The environment variable key.options
(Options): Options for retrieving the value.getHost(key: string, options?: Options): string
Retrieves the specified environment variable value as a host (IP or URL).
key
(string): The environment variable key.options
(Options): Options for retrieving the value.custom(key: string, validator: (key: string, value: any, options: Options) => any, options?: Options | any): any
Retrieves and validates the value of the specified environment variable using a custom validator function.
key
(string): The environment variable key.validator
(function): A custom validation function.options
(Options | any): Options for retrieving the value.clearCache(): void
Clears the environment variable cache.
is(env: string): boolean
Determines if the current environment matches the given value.
env
(string): The environment to check.true
if the current environment matches the given value, otherwise false
.isProduction(): boolean
Determines if the current environment is production.
true
if in a production environment, otherwise false
.isNotProduction(): boolean
Determines if the current environment is not production.
true
if not in a production environment, otherwise false
.isProd(): boolean
Determines if the current environment is prod.
true
if in a prod environment, otherwise false
.isNotProd(): boolean
Determines if the current environment is not prod.
true
if not in a prod environment, otherwise false
.isTesting(): boolean
Determines if the current environment is testing.
true
if in a testing environment, otherwise false
.getEnv(key: string): string | undefined
Retrieves the value of a system environment variable. Works for both Node.js and browser environments.
key
(string): The environment variable key.undefined
if not found.The @stone-js/env
library is a versatile tool for managing environment variables in both JavaScript and TypeScript projects. It provides robust validation and transformation capabilities for different data types, making it easy to ensure that environment configuration values are reliable and properly formatted. With support for custom validation, caching, and multiple data formats, this library simplifies environment management for modern web and server-side applications.
See Contributing Guide.
FAQs
Fluent and simple API to deal with .env file and env variables for both browser and node.js.
The npm package @stone-js/env receives a total of 69 weekly downloads. As such, @stone-js/env popularity was classified as not popular.
We found that @stone-js/env demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.