Skip to content

Commit d47215e

Browse files
fix(ai): direct require('node:fs') in getProcEnv breaks browser bundlers
1 parent 9775f09 commit d47215e

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

packages/ai/src/env-api-keys.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ function getProcEnv(key: string): string | undefined {
8989
if (procEnvCache === null) {
9090
procEnvCache = new Map();
9191
try {
92-
const { readFileSync } = require("node:fs") as typeof import("node:fs");
93-
const data = readFileSync("/proc/self/environ", "utf-8");
94-
for (const entry of data.split("\0")) {
95-
const idx = entry.indexOf("=");
96-
if (idx > 0) {
97-
procEnvCache.set(entry.slice(0, idx), entry.slice(idx + 1));
92+
const fsModule = loadNodeBuiltinModule(NODE_FS_SPECIFIER) as typeof import("node:fs") | null;
93+
if (fsModule) {
94+
const data = fsModule.readFileSync("/proc/self/environ", "utf-8");
95+
for (const entry of data.split("\0")) {
96+
const idx = entry.indexOf("=");
97+
if (idx > 0) {
98+
procEnvCache.set(entry.slice(0, idx), entry.slice(idx + 1));
99+
}
98100
}
99101
}
100102
} catch {

0 commit comments

Comments
 (0)