{"library":"middy-env","title":"Middy Environment Variable Middleware","description":"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.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install middy-env"],"cli":null},"imports":["import env from 'middy-env';","import middy from '@middy/core';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const middy = require('middy');\nconst env = require('middy-env');\n\nconst handler = async (event, context) => {\n  // Access variables directly from context object\n  const firstName = context.firstName ?? 'Guest';\n  const lastName = context.lastName ?? '';\n\n  return {\n    statusCode: 200,\n    body: `Hello ${firstName} ${lastName}`\n  };\n};\n\nmodule.exports = middy(handler)\n  .use(env({\n    names: {\n      firstName: ['FIRST_NAME', 'string', 'World'], // 'World' is fallback if FIRST_NAME env var is missing\n      lastName: 'LAST_NAME' // Will throw ReferenceError if LAST_NAME is missing and no fallback\n    },\n    cache: true,\n    cacheExpiryInMillis: 3600000 // Cache for 1 hour (3,600,000 milliseconds)\n  }));","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}