aws / aws/bedrock-agentcore-sdk-typescript
BedrockAgentCoreApp: createRequire() for @fastify/sse breaks esbuild bundling
- Dominant language
- TypeScript
- Stars
- 91
- Forks
- 31
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 5
Description
## Problem
`BedrockAgentCoreApp` from `bedrock-agentcore/runtime` uses `createRequire()` to load `@fastify/sse` and `@fastify/websocket`:
```js
// dist/src/runtime/app.js
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const fastifySse = require('@fastify/sse');
const fastifyWebsocket = require('@fastify/websocket');
```
This pattern is opaque to esbuild's static analysis ([evanw/esbuild#1828](https://github.com/evanw/esbuild/issues/1828)). When bundling an agent into a single file with esbuild — which is the standard approach for minimal Docker images on AgentCore — `@fastify/sse` and `@fastify/websocket` are not included in the output. The container then crashes on startup:
```
Error: Cannot find module '@fastify/sse'
Require stack:
- /home/nonroot/index.js
```
## Environment
- `bedrock-agentcore`: 0.2.4
- esbuild config: `{ bundle: true, platform: 'node', format: 'esm' }`
- Docker image: distroless Node.js (only `index.js` copied, no `node_modules`)
## Suggested fix
Both `@fastify/sse` (since v4.x) and `@fastify/websocket` support ESM exports. Replace the `createRequire` pattern with direct ESM imports:
```diff
- import { createRequire } from 'module';
- const require = createRequire(import.meta.url);
- const fastifySse = require('@fastify/sse');
- const fastifyWebsocket = require('@fastify/websocket');
+ import fastifySse from '@fastify/sse';
+ import fastifyWebsocket from '@fastify/websocket';
```
This makes the dependencies visible to bundlers (esbuild, Rollup, webpack) and eliminates the runtime `createRequire` workaround entirely.
## Workarounds
Until this is fixed upstream, consumers can either:
1. Mark `@fastify/sse` and `@fastify/websocket` as `external` in esbuild and copy them into the Docker image
2. Drop the single-file bundle approach and vendor `node_modules/` instead
## Related
- #61 (Add dual module support)
- [evanw/esbuild#1828](https://github.com/evanw/esbuild/issues/1828) (`createRequire` not supported by esbuild)
Contributor guide
Research direction
Start by locating the source corresponding to dist/src/runtime/app.js and inspect how BedrockAgentCoreApp loads @fastify/sse and @fastify/websocket. Verify the bundled output with the stated esbuild configuration and confirm that a single-file bundle includes both dependencies and starts without the missing-module error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- backend, build-system
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 75/100