AOT mode incompatible with bun build-time constants
What version of Elysia is running?
2.0.0-beta.3
What platform is your computer?
Darwin 25.6.0 arm64 arm
What environment are you using
Bun version: 1.4.2
Are you using dynamic mode?
no
What steps can reproduce the bug?
AOT mode cannot coexist with bun build‑time constants feature
When using the AOT plugin from Elysia together with Bun.build’s define option, the build‑time constants (e.g., NODE_ENV, BUILD_VERSION, etc.) are not available inside the server code during the AOT compilation step. This results in a runtime error because the constants are treated as undefined.
Steps to reproduce:
- build.ts – the build script:
import { $ } from "bun";
import { aot } from "elysia/plugin/aot/bun";
const version = await $`git describe --tags --always`.text();
const buildTime = new Date().toISOString();
const gitCommit = await $`git rev-parse HEAD`.text();
Bun.build({
entrypoints: ["src/index.ts"],
define: {
NODE_ENV: "production",
BUILD_VERSION: JSON.stringify(version.trim()),
BUILD_TIME: JSON.stringify(buildTime),
GIT_COMMIT: JSON.stringify(gitCommit.trim()),
},
minify: {
whitespace: true,
syntax: true,
},
compile: {
outfile: "server",
},
target: "bun",
plugins: [aot("src/server.ts")],
}).then(() => {
process.exit(0);
});- src/index.ts – the entrypoint:
import cluster from "node:cluster";
import os from "node:os";
import process from "node:process";
if (cluster.isPrimary) {
for (let i = 0; i < os.availableParallelism(); i++) cluster.fork();
} else {
await import("./server");
console.log(`Worker ${process.pid} started`);
}- src/server.ts – the Elysia server:
import { Elysia } from "elysia";
export const app = new Elysia()
.get("health", { isProduction: NODE_ENV, GIT_COMMIT, BUILD_VERSION, BUILD_TIME })
.use(xxx) // placeholder for actual middleware
.listen(process.env.LISTEN_PORT ?? 3000);
export type App = typeof app;- Run the build:
bun run build.tsThe build fails with an error about NODE_ENV is not defined
What is the expected behavior?
A possible solution would be for the AOT plugin to provide an option to either skip the build‑time constants or be compatible with them, so that the constants defined in Bun.build are properly substituted during the AOT compilation phase.
What do you see instead?
No response
Additional information
No response
Have you try removing the node_modules and bun.lock and try again yet?
No response
Source: elysiajs/elysia