{"id":17514,"library":"bragg-env","title":"Bragg Environment Middleware","description":"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.","status":"abandoned","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/SamVerschueren/bragg-env","tags":["javascript","bragg","aws","lambda","environment","env","amazon"],"install":[{"cmd":"npm install bragg-env","lang":"bash","label":"npm"},{"cmd":"yarn add bragg-env","lang":"bash","label":"yarn"},{"cmd":"pnpm add bragg-env","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core framework this middleware extends. Implicit peer dependency.","package":"bragg","optional":false},{"reason":"Internal dependency for environment detection logic.","package":"aws-lambda-env","optional":false}],"imports":[{"note":"bragg-env is a CommonJS module and does not officially support ESM imports. Attempting ESM will likely result in a runtime error.","wrong":"import environment from 'bragg-env';","symbol":"environment","correct":"const environment = require('bragg-env');"},{"note":"While `require('bragg-env')` returns the `environment` function directly, destructuring can technically work in CJS but is not the primary export pattern shown in documentation. ESM named import will fail.","wrong":"import { environment } from 'bragg-env';","symbol":"environment (named)","correct":"const { environment } = require('bragg-env');"}],"quickstart":{"code":"const app = require('bragg')();\nconst environmentMiddleware = require('bragg-env');\n\n// Optionally set NODE_ENV for testing different environments\n// process.env.NODE_ENV = 'test';\n\napp.use(environmentMiddleware({\n  $LATEST: 'development',\n  default: 'production'\n}));\n\napp.use(ctx => {\n  console.log('Detected environment:', ctx.env);\n  // ctx.env will be 'development', 'production', 'test', or another configured value.\n  ctx.res.statusCode = 200;\n  ctx.res.end(`Hello from ${ctx.env} environment!`);\n});\n\n// To run this, you would typically use a Bragg adapter, e.g., 'bragg-lambda'\n// For local testing, you might mock a simple server or use a local Bragg runner.\n// This example primarily demonstrates middleware integration.\n\n// Example local execution (conceptual, requires bragg runner setup):\n// const http = require('http');\n// http.createServer(app.callback()).listen(3000, () => {\n//   console.log('Bragg app listening on http://localhost:3000');\n// });","lang":"javascript","description":"Demonstrates how to integrate `bragg-env` middleware into a `bragg` application to expose the detected environment on the context object."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure `bragg` is installed: `npm install bragg`. If using a monorepo, verify that `bragg` is resolvable from the `bragg-env` context.","cause":"The `bragg` framework, which `bragg-env` is middleware for, is not installed or correctly referenced.","error":"Error: Cannot find module 'bragg'"},{"fix":"Ensure `app` is initialized as `const app = require('bragg')();` before calling `app.use()`.","cause":"The `app` object is not a valid `bragg` application instance, or it has not been initialized correctly.","error":"TypeError: app.use is not a function"},{"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.","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.","error":"ctx.env always returns 'development' or 'production' despite NODE_ENV being set"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}