Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#1994·elysia

AOT mode incompatible with bun build-time constants

Author: Eliot00Created Sep 9, 2026Updated Sep 9, 2026
Labelsbug

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:

  1. build.ts – the build script:
typescript
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);
});
  1. src/index.ts – the entrypoint:
typescript
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`);
}
  1. src/server.ts – the Elysia server:
typescript
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;
  1. Run the build:
bash
bun run build.ts

The 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

View original on GitHubView discussion on GitHub