{"id":11102,"library":"is-lambda","title":"AWS Lambda Environment Detector","description":"is-lambda is a lightweight utility package designed to detect whether the current JavaScript execution environment is an AWS Lambda server. It achieves this by inspecting well-known AWS Lambda-specific environment variables such as `AWS_LAMBDA_FUNCTION_NAME`, `AWS_REGION`, and `LAMBDA_TASK_ROOT`. The current stable version is 1.0.1. Due to its focused and simple nature, its release cadence is typically very slow, with updates primarily occurring if AWS Lambda's environment signature changes or critical bug fixes are required. Its key differentiator is its minimalism, providing a single boolean value without additional overhead or complex configuration, making it suitable for quick environmental checks in serverless applications or conditional logic based on the deployment environment.","status":"maintenance","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/watson/is-lambda","tags":["javascript","aws","hosting","hosted","lambda","detect"],"install":[{"cmd":"npm install is-lambda","lang":"bash","label":"npm"},{"cmd":"yarn add is-lambda","lang":"bash","label":"yarn"},{"cmd":"pnpm add is-lambda","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package exports a boolean directly as module.exports. CommonJS `require` is the primary usage pattern.","symbol":"isLambda","correct":"const isLambda = require('is-lambda');"},{"note":"For ESM, it typically imports the CJS default export. Named imports (`{ isLambda }`) will not work as it's a direct boolean export, not an object.","wrong":"import { isLambda } from 'is-lambda';","symbol":"isLambda","correct":"import isLambda from 'is-lambda';"}],"quickstart":{"code":"const isLambda = require('is-lambda');\n\nfunction main() {\n  if (isLambda) {\n    console.log('The code is running on an AWS Lambda function.');\n    // Example: Accessing Lambda-specific environment variables\n    console.log('Function Name:', process.env.AWS_LAMBDA_FUNCTION_NAME ?? 'N/A');\n    console.log('AWS Region:', process.env.AWS_REGION ?? 'N/A');\n  } else {\n    console.log('The code is NOT running on an AWS Lambda function.');\n    console.log('Simulating local environment...');\n    // Example of local behavior\n    const message = 'Hello from local environment!';\n    console.log(message);\n  }\n}\n\n// To demonstrate local behavior when running outside Lambda:\n// Temporarily clear AWS Lambda specific environment variables\nconst originalLambdaEnv = process.env.AWS_LAMBDA_FUNCTION_NAME;\ndelete process.env.AWS_LAMBDA_FUNCTION_NAME;\nmain();\nprocess.env.AWS_LAMBDA_FUNCTION_NAME = originalLambdaEnv; // Restore if needed\n\n// To demonstrate Lambda behavior (if run on Lambda, or mock locally):\n// process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-test-function';\n// process.env.AWS_REGION = 'us-east-1';\n// main();\n","lang":"javascript","description":"This quickstart demonstrates how to use `is-lambda` to check the execution environment and shows conditional logic for both Lambda and non-Lambda contexts, including accessing common AWS environment variables."},"warnings":[{"fix":"Ensure your `is-lambda` version is up-to-date if AWS makes significant changes to its environment variable landscape. Periodically test your application in new Lambda runtimes.","message":"The detection relies on specific AWS Lambda environment variables (e.g., `AWS_LAMBDA_FUNCTION_NAME`, `AWS_REGION`). While these are stable, changes in future AWS Lambda runtime environments could potentially affect detection accuracy, though this is rare.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually set the necessary AWS Lambda environment variables in your local development environment or test runner (`AWS_LAMBDA_FUNCTION_NAME`, `AWS_REGION`, etc.) to simulate a Lambda environment accurately if your code depends on `is-lambda`.","message":"Local serverless emulation tools (e.g., `serverless-offline`, `SAM CLI local`) might not perfectly replicate all AWS Lambda environment variables, leading to `is-lambda` incorrectly returning `false` in a local 'Lambda-like' environment. This means tests might pass locally but fail when deployed.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always import it as a default export in ESM (`import isLambda from 'is-lambda';`) or use the CommonJS `require` syntax (`const isLambda = require('is-lambda');`).","message":"The package directly exports a boolean value. Attempting to destructure it as a named export (`import { isLambda } from 'is-lambda';`) or use it in other ways expecting an object will result in errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `import isLambda from 'is-lambda';`.","cause":"Attempting to import `is-lambda` as a named export in an ESM context, but the package directly exports a boolean.","error":"TypeError: (0 , _isLambda.isLambda) is not a function"},{"fix":"Ensure you are assigning the result of `require('is-lambda')` directly to a variable: `const isLambda = require('is-lambda');`.","cause":"Incorrectly trying to access `isLambda` as a property of an object when using `require`, or other incorrect CommonJS patterns.","error":"TypeError: Cannot read properties of undefined (reading 'isLambda')"}],"ecosystem":"npm"}