Middy Invocation Middleware
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
A middleware for the Middy framework (v2.5.3+ peer dependency) that provides global access to current AWS Lambda invocation event and context from anywhere in your code, eliminating the need to pass them as arguments through function calls. This is a small, focused utility with no active development since v1.0.0 (2022). Key differentiator: it uses async hooks (presumably) to store the invocation context, enabling access from deeply nested modules. Similar libraries include @middy/core's built-in context sharing, but this offers a simpler getter pattern. Only one known release (v1.0.0) with no changelog or updates.
Common errors
error TypeError: invocation is not a function ↓
cause Trying to use `invocation` as a function after import, but it's already a middleware factory.
fix
Correct: middy(handler).use(invocation())
error ReferenceError: Cannot access 'event' before initialization ↓
cause Attempting to destructure `event` before calling the function, or using it outside of handler scope.
fix
Use
const { event } = require('middy-invocation'); const evt = event(); inside the handler. error Cannot find module 'middy-invocation' ↓
cause Package not installed or peer dependency @middy/core missing.
fix
Run
npm install @middy/core@^2.5.3 middy-invocation Warnings
gotcha The event and context functions must be called after the middleware has run (i.e., only within the handler function or its async chain). ↓
fix Ensure usage is always inside a handler invocation, not at module load time.
deprecated Package requires @middy/core ^2.5.3 peer dependency; no support for @middy/core v3+. ↓
fix Pin @middy/core to ^2.5.3 or migrate to a maintained alternative.
breaking Only CJS exports; no ES module support. ↓
fix Use require() instead of import, or use dynamic import() if your runtime supports it.
gotcha If multiple invocations run concurrently (e.g., with context reuse in warm containers), the stored event/context may be overwritten by a later invocation. ↓
fix Capture event/context immediately in the handler and pass explicitly, or use AsyncLocalStorage for isolation.
gotcha No TypeScript types are provided; using the package in TypeScript requires manual type declarations or @types/middy-invocation (which does not exist). ↓
fix Create a local .d.ts file or use `any` typecasting.
Install
npm install middy-invocation yarn add middy-invocation pnpm add middy-invocation Imports
- invocation wrong
import invocation from 'middy-invocation';correctconst invocation = require('middy-invocation'); - event wrong
import { event } from 'middy-invocation'; const evt = event;correctconst { event } = require('middy-invocation'); const evt = event(); - context wrong
const context = require('middy-invocation').context; const ctx = context;correctconst { context } = require('middy-invocation'); const ctx = context();
Quickstart
const middy = require('@middy/core');
const invocation = require('middy-invocation');
const baseHandler = async (event, context) => {
// Access event and context from anywhere
const { event: getEvent, context: getContext } = require('middy-invocation');
const evt = getEvent();
const ctx = getContext();
console.log('Request ID:', ctx.awsRequestId);
return { statusCode: 200, body: JSON.stringify(evt) };
};
module.exports.handler = middy(baseHandler).use(invocation());