< Back to all hacks

#06 npm Cache on SD Card

Infrastructure
Problem
700 MB internal storage, npm cache + node_modules = 500 MB+. No space for anything.
Solution
Redirect npm cache to SD card: npm config set cache /sdcard/npm-cache
Lesson
Later moved to ext4 rootfs (Hack #31) because FAT32 can't handle deep git paths.

Context

The Moto E2 has 4 GB internal storage, of which only ~700 MB is available to Termux after Android's system partition, app data, and Dalvik cache. npm's default cache location is inside Termux's home directory (~/.npm), and installing OpenClaw with all its dependencies generates 500+ MB of cached packages and node_modules.

With the npm cache on internal storage, there's barely enough room for the rootfs, the installed packages, AND the cache. One failed install attempt can fill the disk and leave Termux in an unrecoverable state.

The phone has a 16 GB micro SD card — plenty of space for caching.

Implementation

Redirect npm's cache to the SD card:

# Point npm cache to SD card
npm config set cache /sdcard/npm-cache

# Create the directory if it doesn't exist
mkdir -p /sdcard/npm-cache

# Verify the setting
npm config get cache
# Expected: /sdcard/npm-cache

For proot environments, the SD card path is bind-mounted:

# Inside proot, the SD card is accessible at the same path
# Verify access:
ls /sdcard/
# Expected: directory listing of SD card contents

# npm install will now cache to SD card
npm install --ignore-scripts --legacy-peer-deps

Verification

# Check cache location:
npm config get cache
# Expected: /sdcard/npm-cache

# Check cache size after install:
du -sh /sdcard/npm-cache
# Expected: 200-400 MB depending on packages

# Verify internal storage freed:
df -h /data/data/com.termux/
# Expected: more available space than before

Gotchas

  • SD card is formatted as FAT32 on Android 6, which has a 255-character path length limit. Git dependencies with deep .git/objects/ hierarchies will fail with ENAMETOOLONG
  • FAT32 doesn't support symlinks, which some npm packages expect
  • SD card I/O is slower than internal storage (~10-20 MB/s vs ~50-100 MB/s)
  • This was later superseded by Hack #31 (npm cache on ext4 rootfs) which solves the FAT32 limitations
  • If the SD card is removed or unmounted, npm operations will fail silently or corrupt the cache

Result

MetricBeforeAfter
Internal storage used by npm~500 MB~50 MB
Available internal space~200 MB~650 MB
Cache location~/.npm (internal)/sdcard/npm-cache