aws-sdk-js-v3: TypeError: Object.defineProperty called on non-object in @aws-sdk/client-s3 v3.758.0
Checkboxes for prior research
- I’ve gone through Developer Guide and API reference
- I’ve checked AWS Forums and StackOverflow.
- I’ve searched for previous similar issues and didn’t find any solution.
Describe the bug
After updating @aws-sdk/client-s3 from 3.701.0 to 3.758.0, my AWS Lambda function started failing with the following error:
{
"errorType": "TypeError",
"errorMessage": "Object.defineProperty called on non-object",
"trace": [
"TypeError: Object.defineProperty called on non-object",
" at defineProperty (<anonymous>)",
" at __name (/var/task/src/functions/hello/handler.js:9789:37)",
" at <static_initializer> (/var/task/src/functions/hello/handler.js:9981:9)",
" at node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js (/var/task/src/functions/hello/handler.js:9976:34)",
" at __require (/var/task/src/functions/hello/handler.js:10:50)",
" at node_modules/@aws-sdk/client-s3/dist-cjs/index.js (/var/task/src/functions/hello/handler.js:21641:37)",
" at __require (/var/task/src/functions/hello/handler.js:10:50)",
" at Object.<anonymous> (/var/task/src/functions/hello/handler.js:31948:32)",
" at Module._compile (node:internal/modules/cjs/loader:1364:14)",
" at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)"
]
}
Regression Issue
- Select this option if this issue appears to be a regression.
SDK version number
@aws-sdk/client-s3@3.758.0
Which JavaScript Runtime is this issue in?
Node.js
Details of the browser/Node.js/ReactNative version
18
Reproduction Steps
- Install the latest version (3.758.0) of @aws-sdk/client-s3:
npm install @aws-sdk/client-s3@latest
- Use the following Lambda function:
import { S3 } from "@aws-sdk/client-s3";
const s3 = new S3({ region: "us-east-1" });
export const handler = async (event: { key: string }) => {
try {
const bucketName = process.env.BUCKET_NAME;
const objectKey = event.key;
if (!bucketName || !objectKey) {
throw new Error("Missing bucket name or object key");
}
const response = await s3.headObject({
Bucket: bucketName,
Key: objectKey,
});
return {
statusCode: 200,
body: JSON.stringify(response),
};
} catch (error: any) {
return {
statusCode: error.$metadata?.httpStatusCode || 500,
body: JSON.stringify({ message: error.message }),
};
}
};
- Deploy the Lambda function and invoke it with a test event:
{
"key": "path/to/object.jpg"
}
- The function fails with
TypeError: Object.defineProperty called on non-object.
Observed Behavior
When executing a simple headObject call to S3 using @aws-sdk/client-s3@3.758.0, the function fails with a TypeError: Object.defineProperty called on non-object.
Full error log from Lambda execution logs:
2025-03-11T00:19:56.203Z undefined ERROR Uncaught Exception {
"errorType": "TypeError",
"errorMessage": "Object.defineProperty called on non-object",
"stack": [
"TypeError: Object.defineProperty called on non-object",
" at defineProperty (<anonymous>)",
" at __name (/var/task/src/functions/hello/handler.js:9789:37)",
" at <static_initializer> (/var/task/src/functions/hello/handler.js:9981:9)",
" at node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js (/var/task/src/functions/hello/handler.js:9976:34)",
" at __require (/var/task/src/functions/hello/handler.js:10:50)",
" at node_modules/@aws-sdk/client-s3/dist-cjs/index.js (/var/task/src/functions/hello/handler.js:21641:37)",
" at __require (/var/task/src/functions/hello/handler.js:10:50)",
" at Object.<anonymous> (/var/task/src/functions/hello/handler.js:31948:32)",
" at Module._compile (node:internal/modules/cjs/loader:1364:14)",
" at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)"
]
}
Expected Behavior
The function should successfully execute and return metadata from headObject, as it does in @aws-sdk/client-s3 version 3.701.0.
Possible Solution
Workaround
Downgrading to version 3.701.0 resolves the issue:
npm install @aws-sdk/client-s3@3.701.0
Additional Information/Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 10
- Comments: 16 (4 by maintainers)
Thanks for the investigation. The recommended fix for this issue appears then to be to upgrade to at least esbuild 0.18.2.
https://github.com/evanw/esbuild/releases/tag/v0.18.2
This is how esbuild 0.18.1 generates DynamoDBDocumentClientCommand in an application:
The AWS SDK dist-cjs artifact:
related changes in esbuild https://github.com/lucacasonato/esbuild/commit/d5b317aba6d800dc9586ab6441440c70b158733b
👎 with
esbuild 0.18.1the output is brokenTypeError: Object.defineProperty called on non-object👍 with
esbuild 0.18.2workscc @vvo
diff outputs between 0.18.1 and 0.18.2; using "@aws-sdk/lib-dynamodb": "3.775.0"
I also got this to work using Node v22 runtime and by excluding @aws-sdk/* from the build process.
However I think this might be covering this issue up. When I log what version of @aws-sdk/client-s3 the current node v22 lambda runtime is using it is 3.726.1
It might be that the bugs were introduced in a later version, and that eventually a runtime update of the @aws-sdk version used will cause these errors to appear even when using the included aws-sdk from the lambda runtime?