/commands/bun/run-typescript-file
bun run index.ts: run a TypeScript file with Bun
- 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
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.tsOutput
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
cat > bad.ts <<'EOF'
const port: number = "8080"; // type error, but valid JavaScript
console.log(port, typeof port);
EOF
npx --yes bun@1 bad.tsOutput
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 $.
| Command | What it runs |
|---|---|
| bun index.ts | The file (shorthand for bun run index.ts) |
| bun run dev | The "dev" script from package.json |
| bun --watch index.ts | The file, restarted on changes |
| bun run --bun vite | A package binary, forcing Bun instead of Node.js for scripts with a node shebang |