Bragg Environment Middleware

raw JSON →
2.0.0 verified Thu Apr 23 auth: no javascript abandoned

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.

error Error: Cannot find module 'bragg'
cause The `bragg` framework, which `bragg-env` is middleware for, is not installed or correctly referenced.
fix
Ensure 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
cause The `app` object is not a valid `bragg` application instance, or it has not been initialized correctly.
fix
Ensure 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
cause The `NODE_ENV` environment variable might not be correctly propagated to the Lambda execution environment or the process where `bragg` is running, or the `options` passed to `environment()` might be overriding `NODE_ENV` behavior.
fix
Verify that 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.
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.
fix Consider migrating to actively maintained serverless frameworks and their respective environment management solutions for new projects. For existing projects, proceed with caution and thorough security audits.
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.
fix There is no direct fix for this within `bragg-env`. If you must use this package, you might be forced to use older Node.js runtimes, which comes with its own security and maintenance risks. It's strongly recommended to avoid this package in environments requiring modern Node.js versions.
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.
fix Always use `const environment = require('bragg-env');` for importing this package. If your project is ESM-first, you might need to use dynamic `import()` or review your build configuration to accommodate CommonJS modules, or consider alternatives.
npm install bragg-env
yarn add bragg-env
pnpm add bragg-env

Demonstrates how to integrate `bragg-env` middleware into a `bragg` application to expose the detected environment on the context object.

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');
// });