Fix macstats native addon load in GPU API route (Apple Silicon)#884
Open
wasawi wants to merge 1 commit into
Open
Fix macstats native addon load in GPU API route (Apple Silicon)#884wasawi wants to merge 1 commit into
wasawi wants to merge 1 commit into
Conversation
The /api/gpu route loaded the native `macstats` addon via
`import { createRequire } from 'module'` + `createRequire(import.meta.url)`.
The Next.js server bundler strips that import (compiling `createRequire` to
`(void 0)`) and rewrites `import.meta.url` to an internal bundle path, so on
macOS the require always threw and the route silently returned 0 for GPU
temperature, utilization, power, and fan speed.
Use `process.getBuiltinModule('module')` (Node >=22.3) — a runtime call the
bundler can't touch — and resolve from `process.cwd()` (the ui/ folder) so
node_modules/macstats is found reliably. GPU stats now populate correctly on
Apple Silicon.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Apple Silicon, the GPU monitor in the UI always shows 0 for temperature, utilization, power, and fan speed.
The
/api/gpuroute loads the nativemacstatsaddon with:When
next buildbundles the server route, it strips thecreateRequireimport (the minified output becomeslet a=(void 0)("macstats")) and rewritesimport.meta.urlto an internal bundle path. So the require throws (TypeError: (void 0) is not a function, orMODULE_NOT_FOUND), thecatchswallows it withconsole.warn('macstats not available', ...), and every stat falls back to 0.The addon itself is installed and working —
require('macstats')succeeds outside the bundle.Fix
Reach
createRequirethroughprocess.getBuiltinModule('module')— a runtime call the bundler cannot strip (Node ≥ 22.3; the UI ships Node 23) — and resolve fromprocess.cwd()(theui/folder, which is the server's working directory) sonode_modules/macstatsis found reliably:Verification
On an M3 Max,
/api/gpubefore the fix returnedtemperature: 0, utilization.gpu: 0, power.draw: 0plus amacstats not availablewarning on every request. After the fix it returns live values during a training run:{"temperature":72,"utilization":{"gpu":100,"memory":79},"power":{"draw":20.49}}with zero warnings.
🤖 Generated with Claude Code