Lesson 3 of 9 · 23m
Execution Model, TypeScript, and Module Resolution
Understand how Bun executes TypeScript and modules so transpile-only behavior, ESM/CJS edges, and path aliases do not surprise you.
Bun makes TypeScript feel instantaneous because it does less than the TypeScript compiler. That sentence saves teams days. Bun transpiles TypeScript: it removes types and transforms syntax so JavaScript can execute. It does not prove your types are correct. If you understand that boundary, Bun is pleasant. If you do not, CI becomes a collection of runtime errors that tsc would have caught for free.
Transpile is not typecheck
This file can execute even though it is type-incorrect:
type User = { id: string; email: string };
const user: User = { id: 42, email: true };
console.log(user.email.toLowerCase());Bun may strip the annotations and reach toLowerCase before anyone notices. Keep the compiler in your pipeline:
{
"scripts": {
"typecheck": "tsc --noEmit",
"test": "bun test",
"ci": "bun run typecheck && bun test"
}
}Fast execution should remove waiting, not verification.
Configure tsconfig.json deliberately
A modern Bun service commonly starts with settings like:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"skipLibCheck": true,
"types": ["bun"],
"paths": {
"@server/*": ["./src/*"]
}
},
"include": ["src", "tests"]
}The important part is not copying this exactly. It is making the compiler, editor, bundler, and runtime agree. If paths works in the editor but the deployed artifact is not bundled, production can fail at startup because raw TypeScript path aliases were never transformed into runtime-resolvable files.
ESM and CommonJS are still real
Bun handles a great deal of interop automatically, but the JavaScript module divide did not disappear. Problems usually appear at package boundaries:
ESM package imports CommonJS dependency usually workable
CommonJS package requires ESM dependency often breaks
package uses __dirname not native to ESM
dependency checks import.meta.url environment-sensitiveKnow which module format your emitted artifact uses. --target bun and bundling hide some friction during development, but deployment is where unbundled node_modules, symlinks, and package export maps become honest.
Watch environment and execution context
Serverless functions, Docker containers, systemd services, and local shells provide different environment variables, file-system layout, signals, and process lifetimes. A TypeScript file that works through bun run dev can still fail as a bundled binary because relative paths move:
const schemaPath = new URL("../sql/schema.sql", import.meta.url);
const schema = await Bun.file(schemaPath).text();import.meta.url makes the relationship to the current module explicit. It is usually safer than assuming the process working directory.
Field notes
transpile-onlybehavior is a feature for speed, not a license to deletetscfrom CI.- Add
"types": ["bun"]deliberately; global type visibility can hide runtime-specific dependencies. - Test the exact artifact you deploy. A dev-server resolution success is not proof of production resolution.
- If a dependency has separate
requireandimportconditions, verify which one Bun resolves in the final bundle.
Was this lesson helpful?