Bragg Environment Middleware
raw JSON →bragg-env is a middleware for the `bragg` serverless framework, designed to automatically extract and expose environment information within the request context (`ctx.env`) for AWS Lambda functions. It leverages the `aws-lambda-env` package to intelligently determine the runtime environment, providing sensible defaults such as `development` when running in `$LATEST` Lambda versions and `production` for other deployed versions. This behavior can be explicitly overridden by setting the `NODE_ENV` environment variable. The package's current stable version is 2.0.0, released approximately four years ago, indicating a mature but inactive state. Both `bragg-env` and its foundational `bragg` framework show no recent development activity or releases, suggesting the package is now abandoned. Its primary utility lies in simplifying environment configuration within the `bragg` ecosystem, abstracting away the specifics of AWS Lambda environment detection, but it lacks modern features and active community support that newer serverless frameworks might offer.
Common errors
error Error: Cannot find module 'bragg' ↓
bragg is installed: npm install bragg. If using a monorepo, verify that bragg is resolvable from the bragg-env context. error TypeError: app.use is not a function ↓
app is initialized as const app = require('bragg')(); before calling app.use(). error ctx.env always returns 'development' or 'production' despite NODE_ENV being set ↓
process.env.NODE_ENV is set *before* the bragg-env middleware is initialized. If running in AWS Lambda, ensure NODE_ENV is configured in the Lambda function's environment variables. Review the options object passed to environment() to confirm default or other keys are not unintentionally preventing NODE_ENV from taking precedence. Warnings
gotcha The `bragg-env` package, along with its core framework `bragg`, appears to be abandoned. The last significant update for both packages was over four years ago, indicating no active development, bug fixes, or security updates. Users should be aware of potential unaddressed vulnerabilities or lack of support for modern Node.js features and practices. ↓
breaking The package's `engines` field specifies `"node": ">=4"`, which is an extremely old Node.js version. Running `bragg-env` on modern Node.js versions (e.g., 16, 18, 20+) may lead to unexpected behavior, runtime errors, or performance issues due to outdated dependencies or incompatible syntax. ↓
gotcha This package is a CommonJS module and does not support ES Modules (ESM) syntax directly. Attempting to use `import environment from 'bragg-env'` or `import { environment } from 'bragg-env'` in an ESM context will result in a runtime error. ↓
Install
npm install bragg-env yarn add bragg-env pnpm add bragg-env Imports
- environment wrong
import environment from 'bragg-env';correctconst environment = require('bragg-env'); - environment (named) wrong
import { environment } from 'bragg-env';correctconst { environment } = require('bragg-env');
Quickstart
const app = require('bragg')();
const environmentMiddleware = require('bragg-env');
// Optionally set NODE_ENV for testing different environments
// process.env.NODE_ENV = 'test';
app.use(environmentMiddleware({
$LATEST: 'development',
default: 'production'
}));
app.use(ctx => {
console.log('Detected environment:', ctx.env);
// ctx.env will be 'development', 'production', 'test', or another configured value.
ctx.res.statusCode = 200;
ctx.res.end(`Hello from ${ctx.env} environment!`);
});
// To run this, you would typically use a Bragg adapter, e.g., 'bragg-lambda'
// For local testing, you might mock a simple server or use a local Bragg runner.
// This example primarily demonstrates middleware integration.
// Example local execution (conceptual, requires bragg runner setup):
// const http = require('http');
// http.createServer(app.callback()).listen(3000, () => {
// console.log('Bragg app listening on http://localhost:3000');
// });