gyp: 'Undefined variable android_ndk_path' when building native modules on Android (Termux) without NDK
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- android, nodejs, python
调研方向
首先在 Termux 上重现该故障,并将 node-gyp 的头文件选择过程追踪到缓存的 common.gypi,尤其关注 OS == "android" 分支及其对 android_ndk_path 的引用。将该路径与已打补丁的 /usr/include/node 头文件以及 --nodedir 变通方案进行比较;完成标准是:没有 NDK 的 Android 不再产生误导性的未定义变量故障,并且所选择的受支持路径已有覆盖。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
Building any native module with node-gyp on Android (Termux) fails with an unhelpful gyp error, even though a working toolchain (clang) is installed:
gyp: Undefined variable android_ndk_path in binding.gyp while trying to load binding.gyp
Environment: Termux on Android 15 (aarch64), Node v26.4.0, npm 12.0.2, clang 21, no NDK (Android NDK is not practical to install on Termux; the system provides its own bionic toolchain).
Root cause chain
- Termux's python3 reports
sys.platform == "android", so gyp selects theandroidflavor. - node-gyp does not use the headers shipped with the Node package (
/usr/include/node/common.gypi, which Termux's packaging already patches for this exact issue); instead it downloads the official Node headers from nodejs.org into~/.cache/node-gyp/<version>/. - The official
common.gypihas anOS == "android"branch that references<(android_ndk_path)/sources/android/cpufeatures. On a plain Android system with no NDK,android_ndk_pathis undefined and gyp aborts with the message above — it reads as an internal gyp bug rather than a missing-NDK situation.
Expected behavior
- Either node-gyp should prefer locally installed Node headers when present (Termux's
/usr/include/nodeis a valid, already-patched header set), or - on Android without an NDK, emit an actionable error (e.g. "building native modules on Android requires the Android NDK, or use the system toolchain via
--nodedir=/usr/include/node") instead ofUndefined variable android_ndk_path.
Workaround (used successfully)
Patch ~/.cache/node-gyp/26.4.0/include/node/common.gypi: in the OS == "android" branch, replace -I<(android_ndk_path)/sources/android/cpufeatures with the system include dir. After that, node-pty and other native modules compile and link fine against the Termux bionic toolchain.
This affects every Termux user who builds native npm modules (node-pty, sharp, koffi, etc.).
- 主要语言
- Python
- 星标
- 10.7k
- 派生
- 1.9k
- 平均合并
- 1 天 4 小时
- 30 天内合并 PR
- 5
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nodejs/node-gyp 的其他 Issue
-
dependencies github_actions
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 4/5 3-5 天 新手友好度 48/100
-
MSYS2
难度 4/5 3-5 天 新手友好度 35/100
-
难度 4/5 3-5 天 新手友好度 48/100
-
难度 5/5 一周以上 新手友好度 10/100
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
stephrobert/dsoxlab#238 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 2/5 1-3 小时 新手友好度 75/100
sublimehq/package_control#1780 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 70/100
nwg-piotr/nwg-displays#145 ·