mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.52k stars 238 forks source link

Support for Termux on Android #777

Open gsmitheidw opened 3 weeks ago

gsmitheidw commented 3 weeks ago

I would like install mermaid-cli on Android in Termux. It's failing on puppeteer install citing "current platform is not supported"

My desire is to be able to create mmd files in vim in Termux and use them in conjunction with pandoc to create documents with embedded diagrams.

npm install @mermaid-js/mermaid-cli

> puppeteer@23.6.1 postinstall
> node install.mjs

file:///data/data/com.termux/files/home/node_modules/puppeteer/lib/esm/puppeteer/node/install.js:43
        throw new Error('The current platform is not supported.');
              ^

Error: The current platform is not supported.
    at downloadBrowsers (file:///data/data/com.termux/files/home/node_modules/puppeteer/lib/esm/puppeteer/node/install.js:43:15)
    at file:///data/data/com.termux/files/home/node_modules/puppeteer/install.mjs:35:3

Node.js v22.8.0
npm error code 1
npm error path /data/data/com.termux/files/home/node_modules/puppeteer
npm error command failed
npm error command sh -c node install.mjs
npm error A complete log of this run can be found in: /data/data/com.termux/files/home/.npm/_logs/2024-10-31T11_07_09_706Z-debug-0.log

Test environment

Termux from the (recommended) F-Droid store

$ termux-info
Termux Variables:
TERMUX_API_VERSION=0.50.1
TERMUX_APK_RELEASE=F_DROID
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=32447
TERMUX_IS_DEBUGGABLE_BUILD=0
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.1
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://mirror.twds.com.tw/termux/termux-main stable main
Updatable packages:
All packages up to date
termux-tools version:
1.44.2
Android version:
13
Kernel build information:
Linux localhost 5.10.168-android12-9-00003-g828a3b1f7462-ab10564101 #1 SMP PREEMPT Wed Jul 26 12:50:50 UTC 2023 aarch64 Android
Device manufacturer:
motorola
Device model:
moto g54 5G
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
Installed termux plugins:
com.termux.api versionCode:51

Solutions explored:

Workaround I've explored include messy workarounds using rooted devices and docker or proot distros which is quite a heavy-handed (particularly in terms of Android resources) solution to installing an otherwise small package. I've tried installing puppeteer-core but that does not work.

Potential Solutions:

aloisklink commented 3 weeks ago
  • Provision of a prebuilt binary mmdc that includes the puppeteer functionality compiled in

We do have an issue for this https://github.com/mermaid-js/mermaid-cli/issues/467, but as it's a non-trivial amount of work, I don't think it would get done anytime soon.

  • An alternative in the code to puppeteer that will allow the npm installation to complete, perhaps using a different dependency.

Unfortunately, mermaid-cli requries Puppeteer (or something similar like Playwright), since Mermaid doesn't support server-side rendering, see https://github.com/mermaid-js/mermaid-cli/issues/750


As an alternate approach, can you install a chromium/firefox browser a different way (e.g. via apt install or apk add?

If so, you can install @mermaid-js/mermaid-cli with PUPPETEER_SKIP_DOWNLOAD=true enabled, e.g.

PUPPETEER_SKIP_DOWNLOAD=true npx @mermaid-js/mermaid-cli --help

Then, make a puppeteerConfig.json file with the executablePath key pointing to your version of chrome/firefox, e.g. like:

https://github.com/mermaid-js/mermaid-cli/blob/90f0bb2ff856a499cc08b0096a170cb73596c081/puppeteer-config.json#L1-L6

Then, you pass it via --puppeteerConfigFile:

PUPPETEER_SKIP_DOWNLOAD=true npx @mermaid-js/mermaid-cli --puppeteerConfigFile puppeteer-config.json

There are some docs in https://pptr.dev/troubleshooting#running-puppeteer-in-docker and https://github.com/puppeteer/puppeteer/issues/9460 that might help.

robertkirkman commented 2 weeks ago

Hello just today I made this tutorial on the subject of Puppeteer on Android that might help

https://gist.github.com/robertkirkman/0c2f3426024069546ed9b7bb2f26cb99

gsmitheidw commented 2 weeks ago

That looks like a good workaround albeit very heavy in terms of storage to install a full desktop environment just to run a small utility.

For anyone already using Termux with desktop this would be ideal.

I think the browser dependency is kinda non-negotiable, but perhaps the X desktop can be removed after getting the mmdc installed?

Hello just today I made this tutorial on the subject of Puppeteer on Android that might help

https://gist.github.com/robertkirkman/0c2f3426024069546ed9b7bb2f26cb99

robertkirkman commented 2 weeks ago

There is currently a Segmentation fault in the command firefox -headless, preventing the opening of firefox without X11 server present. I will try fix it maybe I can I'm not sure. If I can I will send the patch

robertkirkman commented 5 days ago

I am happy to announce good news that I have fixed the Segmentation fault error, and integrated the patch into a PR into the termux-packages repository, and also updated my guide to have a Headless Mode section that has a full code example for using Puppeteer on Android in Headless mode (would start working whenever the PR is accepted and Termux mirrors are updated. If the PR turns out to have bugs or other issues, and can't be accepted to termux-packages yet, then I will eventually post directions for using it as a custom downstream build of Firefox specifically for Headless Node.js Puppeteer on Android, so that the custom build could be used for that purpose without breaking Firefox for other users)