Registry / aws / is-lambda

is-lambda

JSON →
library1.0.1jsnpmunverified

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.

npm install is-lambda
INSTALL
IMPORT
SIG · IS-LAMBDA
I
is-lambda
awsjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

isLambda
const isLambda = require('is-lambda');
The package exports a boolean directly as module.exports. CommonJS `require` is the primary usage pattern.
isLambda
import isLambda from 'is-lambda';
import { isLambda } from 'is-lambda';
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.

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.

const isLambda = require('is-lambda'); function main() { if (isLambda) { console.log('The code is running on an AWS Lambda function.'); // Example: Accessing Lambda-specific environment variables console.log('Function Name:', process.env.AWS_LAMBDA_FUNCTION_NAME ?? 'N/A'); console.log('AWS Region:', process.env.AWS_REGION ?? 'N/A'); } else { console.log('The code is NOT running on an AWS Lambda function.'); console.log('Simulating local environment...'); // Example of local behavior const message = 'Hello from local environment!'; console.log(message); } } // To demonstrate local behavior when running outside Lambda: // Temporarily clear AWS Lambda specific environment variables const originalLambdaEnv = process.env.AWS_LAMBDA_FUNCTION_NAME; delete process.env.AWS_LAMBDA_FUNCTION_NAME; main(); process.env.AWS_LAMBDA_FUNCTION_NAME = originalLambdaEnv; // Restore if needed // To demonstrate Lambda behavior (if run on Lambda, or mock locally): // process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-test-function'; // process.env.AWS_REGION = 'us-east-1'; // main();
Debug
Known issues
gotchaThe 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.
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.
affects: >=1.0.0
gotchaLocal 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.
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`.
affects: >=1.0.0
gotchaThe 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.
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');`).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , _isLambda.isLambda) is not a function
Attempting to import `is-lambda` as a named export in an ESM context, but the package directly exports a boolean.
fix
Change your import statement to `import isLambda from 'is-lambda';`.
TypeError: Cannot read properties of undefined (reading 'isLambda')
Incorrectly trying to access `isLambda` as a property of an object when using `require`, or other incorrect CommonJS patterns.
fix
Ensure you are assigning the result of `require('is-lambda')` directly to a variable: `const isLambda = require('is-lambda');`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Amazon
1
Resources
is-lambda — npm install is-lambda · libregistry