Registry / serialization / typed-env

typed-env

JSON →
library0.2.1jsnpmunverified

typed-env is a TypeScript-first library designed for parsing environment variables with strong type safety. Its current stable version is 2.0.0, which notably incorporates features from `neon-env` following a recent merge, indicating active development. This library provides mechanisms to define schemas for environment variables, ensuring that values are correctly parsed into specified types like numbers or URLs. Key differentiators include support for custom parsers, allowing complex transformations; the ability to define `choices` to restrict variable values to a predefined set of strings; and the flexibility to supply a custom `env` object instead of relying solely on `process.env`. It's particularly useful in applications requiring robust configuration management and type validation at runtime, helping prevent common configuration-related errors.

npm install typed-env
INSTALL
IMPORT
SIG · TYPED-ENV
T
typed-env
serializationjavascriptv0.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createEnv
import { createEnv } from 'typed-env';
const config = require('typed-env').config; // deprecated name and CJS style
`createEnv` was named `config` in versions prior to 2.0.0. Ensure you use named imports as there is no default export.

Demonstrates defining and accessing typed environment variables, including numbers, string choices, and custom URL parsers, using the `createEnv` function with `process.env` and custom environment objects.

import { createEnv } from 'typed-env'; // Define your environment variable schema const env = createEnv({ PORT: { type: 'number', default: 80 }, NODE_ENV: { type: 'string', choices: ['development', 'production'], default: 'development' }, API_URL: { parser: url => new URL(url), default: 'http://localhost:3000' } }); // Access your typed environment variables console.log(`Application running on port: ${env.PORT}`); console.log(`Environment: ${env.NODE_ENV}`); console.log(`API URL hostname: ${env.API_URL.hostname}`); // Example of providing a custom env object (useful for testing or specific runtimes) const testEnv = createEnv({ LOG_LEVEL: { type: 'string', choices: ['info', 'warn', 'error'], default: 'info' } }, { env: { LOG_LEVEL: 'warn' } }); console.log(`Custom log level: ${testEnv.LOG_LEVEL}`);
Debug
Known issues
breakingThe minimum Node.js version requirement has been updated. Version 2.0.0 and later require Node.js `^14.18.0` or `>=16.0.0`.
fix
Ensure your Node.js environment meets the new minimum requirement by upgrading to Node.js 14.18.0+ or 16.0.0+.
affects: >=2.0.0
breakingThe main entry function `config` has been renamed to `createEnv` in version 2.0.0.
fix
Update all calls from `config(...)` to `createEnv(...)` in your codebase, including import statements.
affects: >=2.0.0
gotchaBy default, `typed-env` loads variables from `process.env`. In non-Node.js environments (e.g., certain browser contexts, specific serverless runtimes that don't polyfill `process`), attempting to access `process.env` will result in a `ReferenceError`.
fix
For non-Node.js environments, explicitly pass a custom `env` object to `createEnv` via the `options` parameter, e.g., `createEnv(schema, { env: yourCustomEnvObject });`
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , typed_env_1.config) is not a function
Attempting to use the old `config` function name after upgrading to `typed-env` v2.0.0+.
fix
Change your import and function call from `config` to `createEnv`. For example: `import { createEnv } from 'typed-env';` and `createEnv(...)`.
Error: Value for 'NODE_ENV' must be one of 'development', 'production'.
An environment variable's value does not match the allowed `choices` defined in the schema.
fix
Ensure the environment variable (e.g., `NODE_ENV`) is set to one of the explicitly allowed values ('development', 'production') or update your `createEnv` schema to include the desired value in the `choices` array.
ReferenceError: process is not defined
`typed-env` attempts to access `process.env`, which is a Node.js global, in an environment where it's unavailable (e.g., a browser or specific serverless edge runtimes).
fix
Provide an explicit `env` object as the second argument to `createEnv`: `createEnv(schema, { env: { NODE_ENV: 'production' } });`
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
20
OpenAI (training)
1
Resources
typed-env — npm install typed-env · libregistry