Skip to content

fix(main/nodejs{,-lts}): auto-configure node-gyp for native modules#27724

Open
ggdev-ops wants to merge 3 commits intotermux:masterfrom
ggdev-ops:fix/nodejs-node-gyp-config
Open

fix(main/nodejs{,-lts}): auto-configure node-gyp for native modules#27724
ggdev-ops wants to merge 3 commits intotermux:masterfrom
ggdev-ops:fix/nodejs-node-gyp-config

Conversation

@ggdev-ops
Copy link
Copy Markdown

Configure npm settings in postinst for Termux native compilation:

  • Add python to TERMUX_PKG_DEPENDS
  • Set npm config: python, nodedir, arch, platform
  • Install check-gyp-deps.sh helper script at $PREFIX/share/nodejs/

Enables 'npm install ' without manual configuration.

Configure npm settings in postinst for Termux native compilation:
- Add python to TERMUX_PKG_DEPENDS
- Set npm config: python, nodedir, arch, platform
- Install check-gyp-deps.sh helper script at $PREFIX/share/nodejs/

Enables 'npm install <native-module>' without manual configuration.
Comment thread packages/nodejs/build.sh Outdated

# Target architecture and platform
npm config set arch $NPM_ARCH
npm config set platform android
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you provide an example of what specific npm install command that has an error would be fixed by this change?

I think I remember there being a command that has an error, but I forgot what it was. If you put it in the PR description, that would help.

Comment thread packages/nodejs-lts/build.sh Outdated
# Check for node-gyp build dependencies in Termux
# Usage: $PREFIX/share/nodejs/check-gyp-deps.sh

MISSING=""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this code should be indented to the same level as the other heredoc in this file.

Comment thread packages/nodejs/build.sh Outdated
fi
python tools/install.py install --dest-dir="" --prefix "${TERMUX_PREFIX}" --build-dir "$_BUILD_DIR"

# Install helper script to check node-gyp build dependencies
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is going to help much. It's just going to end up as a script which is neither used by anyone nor that useful. All these dependencies are already in TERMUX_PKG_SUGGESTS, so they should be installed unless the user decides to go without suggested dependencies.

Comment thread packages/nodejs-lts/build.sh Outdated
# linker, which does not use symbols of linked shared libraries when resolving
# symbols on dlopen(). See https://github.com/termux/termux-packages/issues/462.
TERMUX_PKG_DEPENDS="libc++, openssl, c-ares, libicu, libsqlite, zlib"
TERMUX_PKG_DEPENDS="libc++, openssl, c-ares, libicu, libsqlite, zlib, python"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

python is already in TERMUX_PKG_SUGGESTS, no need to keep it in two places

Comment thread packages/nodejs/build.sh Outdated
# linker, which does not use symbols of linked shared libraries when resolving
# symbols on dlopen(). See https://github.com/termux/termux-packages/issues/462.
TERMUX_PKG_DEPENDS="libc++, openssl, c-ares, libicu, libsqlite, zlib"
TERMUX_PKG_DEPENDS="libc++, openssl, c-ares, libicu, libsqlite, zlib, python"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same comment as earlier

@ggdev-ops
Copy link
Copy Markdown
Author

ggdev-ops commented Dec 22, 2025 via email

Configure npm for native module compilation on install:
- Set arch, platform, nodedir for node-gyp
- Set python, cc, cxx, ar, strip paths
- Warn user if build tools are missing

Fixes: npm install <native-module> fails without manual config
@ggdev-ops
Copy link
Copy Markdown
Author

What This PR Solves

Every Termux user who runs npm install <native-module> (sqlite3, bcrypt, sharp, etc.) gets errors like:

  • gyp ERR! Can't find Python
  • gyp ERR! Cannot find compiler
  • gyp ERR! Undefined variable android_ndk_path

Current Workaround (manual, every user must do monthly)

Users must manually export 10+ environment variables in their shell config:

  1. System Update - Update packages
  2. Install Required Packages - nodejs, clang, make, pkg-config, python, ndk-sysroot, binutils, etc.
  3. Export Environment Variables - npm_config_arch, npm_config_platform, npm_config_nodedir, PYTHON, CC, CXX, AR, STRIP, ANDROID_NDK_HOME
  4. Source Configs - Reload shell
  5. Install node-gyp - npm install -g node-gyp

What This PR Does

Automates Step 3 in the postinst script (runs when nodejs is installed):

Setting Purpose
npm config set arch Target CPU (arm64/arm/x64/ia32)
npm config set platform android Target OS
npm config set nodedir Node.js headers location
npm config set python Python for node-gyp
npm config set cc/cxx Clang compiler paths
npm config set ar/strip LLVM binary tools
Warning message If clang/python/make missing, show pkg install hint

Addressed Review Feedback

  • ✅ Removed python from TERMUX_PKG_DEPENDS (already in SUGGESTS)
  • ✅ Removed helper script (inline warning in postinst instead)
  • ✅ Uses runtime $PREFIX instead of build-time paths

@robertkirkman
Copy link
Copy Markdown
Member

Just npm install -g node-gyp by itself does not seem sufficient to reproduce the error in a clean Termux,

I see this

~ $ npm install -g node-gyp

added 63 packages in 3s

6 packages are looking for funding
  run `npm fund` for details
~ $ 

I do remember there was an error with a different command, but I still don't remember what command that was, could you clarify that?

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.

3 participants