Skip to content

fix(kubernetesjs): resolve node:fs at runtime so the ops-dashboard bundle builds - #42

Merged
pyramation merged 1 commit into
mainfrom
fix/client-no-static-node-fs
Oct 20, 2026
Merged

pyramation merged 1 commit into
mainfrom
fix/client-no-static-node-fs

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

Broken today: Dashboard E2E Tests on main fails at Build all packages (run). #40 made @kubernetesjs/ops reuse the APIClient from kubernetesjs. That client has a static require('node:fs') (in readServiceAccountToken / getServiceAccountCAPath), and it now gets pulled into the ops-dashboard client bundle:

Module build failed: UnhandledSchemeError: Reading from "node:fs" is not handled by plugins
Import trace: node:fs ← packages/kubernetesjs/dist/client.js ← packages/ops/dist/esm/index.js ← ./k8s/context.tsx

#40 itself didn't trip this: the dashboard workflow only runs when apps/ops-dashboard/** changes, and pnpm build skips the dashboard. The next release commit bumped the dashboard's version, which ran the workflow and exposed the failure.

Change: a small nodeFs() helper loads fs at runtime through process.getBuiltinModule('node:fs'), so bundlers never see the import. Outside Node it throws, and the existing try turns that into undefined. The "Node.js only" contract is unchanged.

Side effect (bug fix): require is undefined in the ESM build, so readServiceAccountToken always returned undefined there. It now reads the token.

Verified locally (Node 24, pnpm 10.12.2):

  • pnpm build:all: before the fix it fails with the same UnhandledSchemeError; after, it prints ✓ Compiled successfully and exits 0.
  • readServiceAccountToken('/tmp/satok'): CJS returns tok123 (a missing file returns undefined). ESM returned undefined before the fix and tok123 after.

process.getBuiltinModule needs Node ≥ 20.16 / 22.3. Node 18 and 20 are EOL, and CI runs the latest 20.x.

Link to Devin session: https://app.devin.ai/sessions/ce051f98f6454105bacf2a400f950e64
Open in Devin Desktop: https://app.devin.ai/desktop/session/ce051f98f6454105bacf2a400f950e64?variant=devin
Requested by: @pyramation

PR #40 made @kubernetesjs/ops import kubernetesjs's APIClient, whose static
require('node:fs') then reached the ops-dashboard client bundle and failed
next build with UnhandledSchemeError. process.getBuiltinModule keeps fs out
of the bundler graph, and also makes the ESM build read the service-account
token (require is undefined there, so it always returned undefined).
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

  • 🟠 nodeFs() breaks in-cluster token reads on older Node — client.ts:7

The change introduces a nodeFs() helper that resolves the fs module lazily via process.getBuiltinModule?.('node:fs'), keeping readServiceAccountToken and getServiceAccountCAPath behavior otherwise intact while making the module browser-bundler friendly.

Files Change
packages/kubernetesjs/src/client.ts Adds nodeFs() using process.getBuiltinModule?.('node:fs') instead of a static require, with both in-cluster readers swallowing resolution failures as before.

The intent (avoiding bundler errors on node:fs in browser builds) is sound, but process.getBuiltinModule only exists from Node v22.3.0 / v20.16.0, and the previous require worked on all versions, so the runtime floor silently rises without an engines declaration.

Reviewed commit: fc68797

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR replaces the static require('node:fs') in the in-cluster config path of packages/kubernetesjs/src/client.ts with a process.getBuiltinModule?.('node:fs') helper so browser bundlers don't try to resolve node:fs, but it silently breaks token/CA reads on Node versions older than v20.16/v22.3.

Key findings

  • 🟠 nodeFs() breaks in-cluster token reads on older Node — client.ts:7

Comment on lines +7 to +13
function nodeFs(): typeof import('node:fs') {
const fs = typeof process !== 'undefined' ? process.getBuiltinModule?.('node:fs') : undefined;
if (!fs) {
throw new Error('APIClient: node:fs is not available in this runtime');
}
return fs;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 bug · high

nodeFs() breaks in-cluster token reads on older Node

The new nodeFs() helper resolves fs solely via process.getBuiltinModule?.('node:fs') (packages/kubernetesjs/src/client.ts:8), an API that only exists from Node v22.3.0 (backported to v20.16.0); the removed code used require('node:fs'), which worked on all versions. On Node 18.x and Node 20.0–20.15 the optional-chain yields undefined, so nodeFs() throws — and both readServiceAccountToken (line 165) and getServiceAccountCAPath (line 179) catch that throw in the same bare catch designed for ENOENT and return undefined. In-cluster auth therefore silently degrades, and getInClusterConfig (line 199) reports a misleading "service account token not found. Are you running inside a Kubernetes pod?" even inside a real pod. There is no engines field declaring the new runtime floor, so the regression is silent.

📋 Prompt for AI Agents

In packages/kubernetesjs/src/client.ts lines 7-13, make nodeFs() resilient on Node versions before v20.16/v22.3 where process.getBuiltinModule does not exist: try process.getBuiltinModule?.('node:fs') first, and when it returns undefined fall back to require('node:fs') guarded by typeof require === 'function' (kept dynamic so bundlers still do not statically see a node:fs import), throwing the 'node:fs is not available' error only if both paths fail. Alternatively, if dropping older Node is intentional, add an engines field declaring node >= 20.16 to packages/kubernetesjs/package.json so the requirement is explicit instead of failing silently at runtime.

@pyramation
pyramation merged commit 420ea78 into main Oct 20, 2026
6 checks passed
@pyramation
pyramation deleted the fix/client-no-static-node-fs branch October 20, 2026 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant