aws / aws/aws-cdk

(aws-lambda-nodejs): Wrong @aws-sdk bundling when using format: OutputFormat.ESM and externalModules: []

Open
#29,310 11 comments 0 reactions 1 assignee Claimed by @scanlonp View on GitHub
@aws-cdk/aws-lambda-nodejs bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### Describe the bug

We have a project using esModules. We want to utilize esModules also for our Lambdas.
When using `NodeJsFunction` and provide the following custom bundling options: `bundling: { externalModules: [], format: OutputFormat.ESM },` the resulting `.mjs` file contains wrong imports (dist-cjs instead of dist-es).

Here is the section of the bundled `.mjs` file
```javascript
2024-02-29T13:52:13.280Z undefined ERROR Uncaught Exception
{
"errorType": "Error",
"errorMessage": "Dynamic require of \"os\" is not supported",
"stack": [
"Error: Dynamic require of \"os\" is not supported",
" at file:///var/task/index.mjs:12:9",
" at node_modules/@smithy/shared-ini-file-loader/dist-cjs/getHomeDir.js (file:///var/task/index.mjs:1877:16)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/shared-ini-file-loader/dist-cjs/index.js (file:///var/task/index.mjs:1991:29)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/node-config-provider/dist-cjs/index.js (file:///var/task/index.mjs:2152:41)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/middleware-endpoint/dist-cjs/adaptors/getEndpointFromConfig.js (file:///var/task/index.mjs:2231:34)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/middleware-endpoint/dist-cjs/index.js (file:///var/task/index.mjs:2516:40)"
]
}
```

When setting only format option `bundling: { format: OutputFormat.ESM },`, everything works as expected, because the provided aws-sdk is loaded (instead of the bundled one).

We set `externalModules: []` because that leads to lower cold start times (reference: https://github.com/aws/aws-cdk/issues/25492), which means the `@aws-sdk` is bundled.

### Expected Behavior

I expect that no CommonJS will be used when setting `OutputFormat.esm` in the bundling options.

### Current Behavior

The resulting .mjs file contains commonJs references, which leads to the function crashing:

```javascript
// node_modules/@aws-sdk/util-user-agent-node/dist-cjs/index.js
var require_dist_cjs42 = __commonJS({
"node_modules/@aws-sdk/util-user-agent-node/dist-cjs/index.js"(exports, module) {
```

The function crashes with the following exception:
```
2024-02-29T13:52:13.280Z undefined ERROR Uncaught Exception
{
"errorType": "Error",
"errorMessage": "Dynamic require of \"os\" is not supported",
"stack": [
"Error: Dynamic require of \"os\" is not supported",
" at file:///var/task/index.mjs:12:9",
" at node_modules/@smithy/shared-ini-file-loader/dist-cjs/getHomeDir.js (file:///var/task/index.mjs:1877:16)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/shared-ini-file-loader/dist-cjs/index.js (file:///var/task/index.mjs:1991:29)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/node-config-provider/dist-cjs/index.js (file:///var/task/index.mjs:2152:41)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/middleware-endpoint/dist-cjs/adaptors/getEndpointFromConfig.js (file:///var/task/index.mjs:2231:34)",
" at __require2 (file:///var/task/index.mjs:18:50)",
" at node_modules/@smithy/middleware-endpoint/dist-cjs/index.js (file:///var/task/index.mjs:2516:40)"
]
}
```

### Reproduction Steps

1. Run cdk synth on that project: https://github.com/WtfJoke/cdk-nodejs-esm-bug-reproducer
2. Inspect the resulting .mjs file

Alternatively:
1. Deploy that project: https://github.com/WtfJoke/cdk-nodejs-esm-bug-reproducer
2. Run the `CdkWorkshopStack-MyLambda` lambda and inspect the error

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

10.2.4

### Framework Version

_No response_

### Node.js Version

v20.11.0

### OS

windows (wsl)

### Language

TypeScript

### Language Version

5.3.3

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.