BoundaryML/baml · error · Error
Cannot import ${name} from '@boundaryml/baml' in browser env
Error message
Cannot import ${name} from '@boundaryml/baml' in browser environment. Please import from '@boundaryml/baml/browser' instead. What it means
The @boundaryml/baml main entry point detects a browser (non-Node) environment and replaces exports like Image with getters that throw. The full runtime requires Node native bindings, so browser code must import the browser-safe subset from '@boundaryml/baml/browser'.
Source
Thrown at engine/language_client_typescript/index.js:25
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BamlCtxManager = exports.BamlStream = exports.TraceStats = exports.Timing = exports.StreamTiming = exports.Usage = exports.LlmStreamCall = exports.LlmCall = exports.FunctionLog = exports.Collector = exports.ClientRegistry = exports.invoke_runtime_cli = exports.Video = exports.Pdf = exports.Audio = exports.Image = exports.FunctionResultStream = exports.FunctionResult = exports.BamlRuntime = void 0;
__exportStar(require("./safe_imports"), exports);
__exportStar(require("./errors"), exports);
__exportStar(require("./logging"), exports);
// Detect if we're in a Node.js environment
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
if (!isNode) {
const browserError = (name) => {
throw new Error(`Cannot import ${name} from '@boundaryml/baml' in browser environment. Please import from '@boundaryml/baml/browser' instead.`);
};
// Provide helpful error messages for browser imports
Object.defineProperty(exports, 'Image', {
get: () => browserError('Image'),
enumerable: true,
});
Object.defineProperty(exports, 'Audio', {
get: () => browserError('Audio'),
enumerable: true,
});
Object.defineProperty(exports, 'Pdf', {
get: () => browserError('Pdf'),
enumerable: true,
});
Object.defineProperty(exports, 'Video', {
get: () => browserError('Video'),
enumerable: true,
});View on GitHub (pinned to bd85ce9dee)
Solutions
- Change the import to `import { Image } from '@boundaryml/baml/browser'`.
- Move BAML calls (b.FunctionName(...)) to a server-side module / API route and call your backend from the browser.
- Configure your bundler alias so '@boundaryml/baml' resolves to its browser build only for allowed symbols.
Example fix
// before
import { Image } from '@boundaryml/baml';
// after
import { Image } from '@boundaryml/baml/browser'; Defensive patterns
Strategy: fallback
Validate before calling
const isBrowser = typeof process === 'undefined' || process.versions?.node == null;
const mod = isBrowser ? await import('@boundaryml/baml/browser') : await import('@boundaryml/baml'); Type guard
function isNodeRuntime(): boolean {
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
} Try / catch
let Baml;
try {
Baml = await import('@boundaryml/baml');
} catch (e) {
if (String(e.message).includes("in browser environment")) {
Baml = await import('@boundaryml/baml/browser');
} else throw e;
} Prevention
- Only import BAML in server-side code (API routes, server actions, background jobs).
- Keep any frontend that needs BAML results calling your own backend endpoint, not the library.
- Check your bundler's browser-field resolution for '@boundaryml/baml'.
When it happens
Trigger: Importing anything (e.g. Image, BamlImage) from '@boundaryml/baml' in a bundler targetting browser (webpack/vite/esbuild with browser field) or running in Deno/worker where `process.versions.node` is undefined.
Common situations: Using BAML client code in a React/Next.js client component, a browser extension, or an SSR bundler resolving the browser condition; importing Image/Pdf helpers in frontend code instead of only calling a backend API.
Understand the failure class
Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.
Related errors
- Cannot import ${name} from '@boundaryml/baml' in browser env
- Missing window object
- Environment variable {key} not set
- AWS region is required to build modular request. Set it in t
- LLM client '{client_name}' requires environment variable '{k
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/ce16cb0f3ab75ce1.
Report an issue: GitHub.