Add "bun" export condition so standalone bun compile can embed tiktoken_bg.wasm
Summary
When a Bun project that depends on tiktoken is compiled to a standalone binary with bun build --compile, the resulting binary fails at startup with Error: Missing tiktoken_bg.wasm.
Cause
tiktoken's package.json exports resolve the "node" condition before "default", and Bun (advertising Node compatibility) picks the "node" entry:
".": {
"types": "./tiktoken.d.ts",
"edge-light": "./tiktoken.js",
"node": "./tiktoken.cjs",
"default": "./tiktoken.js"
}
tiktoken.cjs locates the WASM at runtime via:
fs.readFileSync(path.join(__dirname, "./tiktoken_bg.wasm"))
bun build --compile bakes __dirname into the bundle as a literal build-machine path (the host's node_modules/tiktoken directory). It does not embed the WASM asset, because fs.readFileSync on an absolute path is not a pattern Bun's bundler recognises as an asset reference.
Result: the "standalone" binary is not standalone. It silently depends on the build-machine's node_modules/tiktoken/tiktoken_bg.wasm still existing at the same absolute path at runtime — which breaks the moment the binary is distributed, the repo is moved, or node_modules/ is deleted.
In contrast, the ESM tiktoken.js loads the WASM via:
import * as wasm from "./tiktoken_bg.wasm";
which Bun's bundler recognises as an asset import and embeds into the standalone binary. This entry works correctly under bun build --compile.
Reproduction
- In a Bun project:
bun add tiktoken src/index.ts:import { encoding_for_model } from "tiktoken";bun build src/index.ts --compile --target=bun-linux-x64 --outfile dist/app- Move or copy
dist/appanywhere off the build machine. - Run
dist/app→Error: Missing tiktoken_bg.wasm.
Bun version: 1.3.12. tiktoken version: 1.0.22.
Proposed fix
Add a "bun" condition, placed before "node", pointing at the ESM loader. Bun always applies the "bun" condition (both runtime and compile), so it will prefer this over "node", while Node.js resolution is unchanged.
".": {
"types": "./tiktoken.d.ts",
"edge-light": "./tiktoken.js",
"bun": "./tiktoken.js",
"node": "./tiktoken.cjs",
"default": "./tiktoken.js"
}
Same addition for ./init, ./load, ./lite, ./lite/init, ./lite/load, and each ./encoders/<name> entry that currently has "node"/"default" split.
Workaround (for downstream users hitting this today)
Patch your own node_modules/tiktoken/package.json via a postinstall script, or avoid the CJS path by importing the ESM entry directly:
import { encoding_for_model } from "tiktoken/tiktoken.js";