Skip to content

/commands/bun/run-typescript-file

bun run index.ts: run a TypeScript file with Bun

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Bun
versions
Bun 1.x
os
linux, macos, windows

bun run index.ts, or the shorter bun index.ts, runs a TypeScript (or TSX, JSX, JavaScript) file directly. Bun's built-in transpiler removes the type annotations on the fly, so there is no tsc build step and no ts-node. It does not type-check: a type error still runs. Keep tsc --noEmit in your test or CI step to catch type errors.

Run a file

bash
cat > hello.ts <<'EOF'
interface User { name: string; age: number }
const user: User = { name: "Ada", age: 36 };
console.log(`${user.name} is ${user.age}`);
EOF
npx --yes bun@1 run hello.ts
npx --yes bun@1 hello.ts

Output

Ada is 36
Ada is 36

Here Bun is started through npx bun@1 so the example needs only Node.js; with Bun installed, the commands are bun run hello.ts and bun hello.ts.

Types are stripped, not checked

bash
cat > bad.ts <<'EOF'
const port: number = "8080";   // type error, but valid JavaScript
console.log(port, typeof port);
EOF
npx --yes bun@1 bad.ts

Output

8080 string

To get type errors, run the TypeScript compiler without emitting files, for example bunx tsc --noEmit (or npx tsc --noEmit) with a tsconfig.json.

bun run with package.json scripts

bun run NAME looks for a script called NAME in package.json first, then for a file. Bun prints the command it runs, prefixed with $.

CommandWhat it runs
bun index.tsThe file (shorthand for bun run index.ts)
bun run devThe "dev" script from package.json
bun --watch index.tsThe file, restarted on changes
bun run --bun viteA package binary, forcing Bun instead of Node.js for scripts with a node shebang

Sources