Registry / aws / middy-env

middy-env

JSON →
library2.0.0jsnpmunverified

middy-env is a middleware for the middy framework, specifically designed to parse and inject environment variables into AWS Lambda function handlers. It's currently at version 2.0.0, which aligns with the major version 2.x of the core middy framework, ensuring compatibility with the latest middy features and architecture. This package provides robust configuration management for serverless functions, allowing developers to define required environment variables, specify their expected types (string, int, float, bool), and provide fallback values to prevent runtime errors. A key differentiator is its ability to assign parsed variables directly to the Lambda `context` object by default, rather than polluting the global `process.env`, offering a cleaner and more isolated configuration scope per invocation. It also supports optional caching of parsed values to optimize performance for subsequent warm invocations, with configurable expiry times. The package maintains a stable release cadence, typically aligning with major middy framework updates.

npm install middy-env
INSTALL
IMPORT
SIG · MIDDY-ENV
M
middy-env
awsjavascriptv2.0.0
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.

env
import env from 'middy-env';
const { env } = require('middy-env');
The middleware function is the default export. For CommonJS environments, use `const env = require('middy-env');` directly to get the middleware function.
middy
import middy from '@middy/core';
const middy = require('middy');
While `middy-env`'s quickstart uses `require('middy')` for backward compatibility or CJS setups, `middy` v2.x (which `middy-env` v2.x supports) typically recommends `import middy from '@middy/core';` for ESM and modern CJS usage.

Demonstrates how to apply the `middy-env` middleware to a Lambda handler, parsing `FIRST_NAME` and `LAST_NAME` environment variables and injecting them into the `context` object, with caching enabled.

const middy = require('middy'); const env = require('middy-env'); const handler = async (event, context) => { // Access variables directly from context object const firstName = context.firstName ?? 'Guest'; const lastName = context.lastName ?? ''; return { statusCode: 200, body: `Hello ${firstName} ${lastName}` }; }; module.exports = middy(handler) .use(env({ names: { firstName: ['FIRST_NAME', 'string', 'World'], // 'World' is fallback if FIRST_NAME env var is missing lastName: 'LAST_NAME' // Will throw ReferenceError if LAST_NAME is missing and no fallback }, cache: true, cacheExpiryInMillis: 3600000 // Cache for 1 hour (3,600,000 milliseconds) }));
Debug
Known issues
breakingmiddy-env v2.x introduces support for middy v2.x. If your project uses middy v1.x, you must use middy-env v1.x to ensure compatibility.
fix
Upgrade your middy framework to v2.x (`npm install @middy/core@^2`) or downgrade middy-env to v1.x (`npm install middy-env@^1`).
affects: >=2.0.0
gotchaEnvironment variables specified in the `names` option will cause a `ReferenceError` if they are not set in the environment and no fallback value is provided in the configuration.
fix
Always provide a fallback value in your `names` configuration (e.g., `['KEY', 'string', 'defaultValue']`) or guarantee the environment variable is always present in your deployment environment.
affects: >=1.0.0
gotchaBy default, parsed environment variables are assigned to the Lambda `context` object. They are not added to `process.env`.
fix
If you require the parsed variables to be available on `process.env`, set the `setToContext` option to `false` in your middleware configuration (e.g., `env({ names: { ... }, setToContext: false })`).
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: LAST_NAME is not defined
The environment variable 'LAST_NAME' was not found in the Lambda execution environment, and no fallback value was specified in the `middy-env` middleware configuration for `lastName`.
fix
Update your `middy-env` configuration to include a fallback value for `lastName` (e.g., `lastName: ['LAST_NAME', 'string', '']`) or ensure the `LAST_NAME` environment variable is correctly set in your Lambda function's environment.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
middyrequiredPeer dependency; middy-env is middleware for the middy framework.
Agent activity
12 hits · last 30 days
node
12
Resources
middy-env — npm install middy-env · libregistry