I just had a jaw-drop moment - Termux allows you to install NodeJS on an Android device!

It seems to work for a basic Websocket Speed Test I had on hand. The http served by it can be accessed both locally and on the network.

There is a medium post that explains the installation process

Basically: 1. Install termux 2. apt install nodejs 3. node it up!

One restriction I've run into - it seems the shared folders don't have the necessary permissions to install modules. It might just be a file permission thing. The private app storage works just fine.

Answer from Pyro on Stack Overflow
Top answer
1 of 6
128

I just had a jaw-drop moment - Termux allows you to install NodeJS on an Android device!

It seems to work for a basic Websocket Speed Test I had on hand. The http served by it can be accessed both locally and on the network.

There is a medium post that explains the installation process

Basically: 1. Install termux 2. apt install nodejs 3. node it up!

One restriction I've run into - it seems the shared folders don't have the necessary permissions to install modules. It might just be a file permission thing. The private app storage works just fine.

2 of 6
24

You can use Node.js for Mobile Apps.

It works on Android devices and simulators, with pre-built binaries for armeabi-v7a, x86, arm64-v8a, x86_64. It also works on iOS, though that's outside the scope of this question.

Like JXcore, it is used to host a Node.js engine in the same process as the app, in a dedicated thread. Unlike JXcore, it is basically pure Node.js, built as a library, with a few portability fixes to run on Android. This means that it's much easier to keep the project up to date with mainline Node.js.

Plugins for Cordova and React Native are also available. The plugins provide a communication layer between the JavaScript side of those frameworks and the Node.js side. They also simplify development by taking care of a few things automatically, like packaging modules and cross-compiling native modules at build time.

Full disclosure: I work for the company that develops Node.js for Mobile Apps.

๐ŸŒ
Google Play
play.google.com โ€บ store โ€บ apps โ€บ details
Node.js Libraries and Compiler - Apps on Google Play
You can compile node.js code within this app. No secondary setup or installation is needed. Compilation is fast and takes only seconds. You can create multiple node.js files. You can make use of intellisense as you type your code.
Rating: 3.9 โ€‹ - โ€‹ 39 votes
๐ŸŒ
Goland
goland.org โ€บ nodejsonandroid
Building and running Node.js for Android โ€“ Stuff Yaron Finds Interesting
Then I went in via โ€œadb shellโ€ and ran โ€œ/data/local/tmp/Release/node helloworld.jsโ€ ยท And yes, it worked! I even tested it by going to the browser on the phone and navigating to http://localhost:8000. To kill things I just ctrl-c which does kill the adb shell but also the node app. Good enough for now. In theory one should be able to use NPM on the Linux box and then just move the whole thing over to Android and run it there. But this only works if none of the dependencies use an add-on. An add-on requires compiling C code into a form Android can handle.
๐ŸŒ
GitHub
github.com โ€บ sjitech โ€บ build-nodejs-for-android
GitHub - sjitech/build-nodejs-for-android: Build nodejs for android(arm,arm64,x86,x64,mipsel) perfectly and provide prebuilt binaries, and a docker image as workbench.
Build nodejs for android(arm,arm64,x86,x64,mipsel) perfectly and provide prebuilt binaries, and a docker image as workbench. - sjitech/build-nodejs-for-android
Starred by 111 users
Forked by 30 users
Languages ย  Shell
๐ŸŒ
sisik
sisik.eu โ€บ blog โ€บ android โ€บ other โ€บ build-node-js-for-android
Building Node.js for Android | sisik
According to 'BUILDING.md' document found in the source tree, Android is not officially supported. However, there is a android-configure script that aids in building Node.js for Android.
๐ŸŒ
GitHub
github.com โ€บ node-on-mobile โ€บ node-on-android
GitHub - node-on-mobile/node-on-android: Make Node.JS apps for Android
Make Node.JS apps for Android. Contribute to node-on-mobile/node-on-android development by creating an account on GitHub.
Starred by 1.1K users
Forked by 88 users
Languages ย  C 74.7% | C++ 24.3%
๐ŸŒ
GitHub
github.com โ€บ android-js โ€บ androidjs
GitHub - android-js/androidjs: Platform to build android app using node js
It is based on Node.js. It allows you to write fully featured android application in node js and provide you environment to use any npm package in your android app (i.e. SocketIO, fs, etc..) Discord Server for support and discussion Slack Channel for support and discussion
Starred by 479 users
Forked by 107 users
Languages ย  JavaScript 51.1% | TypeScript 47.4% | HTML 1.5%
๐ŸŒ
Android-js
android-js.github.io
Android JS
Android JS provides Node JS runtime environment, So you can write your code in Node.JS and can use any `npm` package which helps to build your app in a quick way. We provides socket.io IPC for front and back process communication
Find elsewhere
Top answer
1 of 5
18

Investigating viable options

[NOTE This answer contains findings that were in the original question]

I have investigated the various options a bit more and here are some preliminary findings.

0. Compiling NodeJS

Each of the options uses some form of NodeJS compiled for Android. But to use any option you would probably want to compile to different Node, Android and architecture (x86, ARM, ARM64, etc.) versions.

This is problematic. NodeJS has an android-configure script, but this results in errors in most combinations I've tried. I created a number of github issues for a working build script. In this issue results are collected:

  • Working build script for Android ARM Node 7.x or 8.x shared library

To summarize:

  • shared library builds all fail (except when building physically on your android, see below)
  • J2V8 with NodeJS (libnode.a) statically linked in libj2v8.so works for 7.x up to 7.9.0
  • build-as-node-executable works for 7.x (using dna2oslab build script)

One interesting workaround was used by @mafintosh: transfer Node to device using Termux and do the compilation there (needs much space and time, but works).

1. Running V8 javascript engine which includes NodeJS (J2V8)

J2V8 is a set of Java bindings for V8. J2V8 focuses on performance and tight integration with V8. [...] [which] forces a more static type system between the JS and Java code, but it also improves the performance since intermediate Objects are not created. [...]

Building J2V8 requires building both the native parts and the Java library (.jar/.aar file). To build the native parts we first build node.js as a library and then statically link J2V8 to that. [...]

For cross-compiling J2V8 uses Docker (android, linux, windows) and Vagrant (macos).

See slideshare: Running NodeJS in a Java World (or see InfoQ video, 32min.)

Features:

  • replace JavaScriptCore engine with more powerful v8 (with NodeJS)
  • multi-threading (threads/workers) support via added J2V8 JNI / Java layer
    • every thread can have its own Isolated V8 Instance
  • 2-way js-to-java bridge (call java from script and vice versa)
  • 2-way integrated error / exception handling
  • beautiful cross-compiling interactive build system (in the works)
  • chrome debugging support
  • others, typed arrays, ES6 support, ...

Characteristics:

  • Specify the versions to compile in build_system/build_settings.py
  • Start a build simply with python build.py --interactive, select build:

    [0] Docker >> android-x86 >> NODE_ENABLED
    [1] Docker >> android-arm >> NODE_ENABLED
    [2] Docker >> alpine-linux-x64 >> NODE_ENABLED
    [3] Docker >> linux-x64 >> NODE_ENABLED
    [4] Docker >> linux-x86 >> NODE_ENABLED
    [5] Vagrant >> macosx-x64 >> NODE_ENABLED
    [6] Vagrant >> macosx-x86 >> NODE_ENABLED
    [7] Native >> windows-x64 >> NODE_ENABLED
    [8] Docker >> windows-x64 >> NODE_ENABLED
    [9] Vagrant >> windows-x64 >> NODE_ENABLED
    
  • Select build steps (or all):

    NodeJS --> CMake --> JNI --> Optimize --> Java/Android --> JUnit
    
  • Compiles V8 as shared library libj2v8_{platform}_{abi}.{ext}

    • Note: nodejs build step cannot build Node shared library (errors), creates static libnode.a to be linked in libj2v8.so
  • Has a JNI layer to make large parts of v8 accessible by Java
  • Additional features (e.g. JS <--> Java bridge) implemented in Java
  • Final build output is a Gradle .aar to include as project dependency

Pros:

  • Relatively active project
  • Good quality code including Java unit tests
  • Adds full power of Java to your app design toolkit
  • Great, intuitive build system (once finished)

Cons:

  • Little, mostly outdated usage documentation
    • Especially undocumented is usage in large(r)-scale JS projects
  • Lot of JNI glue code that must be maintained
  • Project not well-maintained (many old open issues, non-merged PR's)
    • Some PR's hang around for 2 years without even getting a response. Not good
  • Harder to understand J2V8 project setup (many files) than other options
  • Licensing issue ("All rights reserved" in EPL 1.0 license)

2. Use NodeJS directly, embedded as native library (node-on-android)

Node on android works by running your Node.js inside the android app using a shared library. It then bundles a WebView that hosts your UI code. All UI is just classic html/css/js.

In the node app you can require node-on-android to get access to the WebView. You can use this to load an html page in the WebView.

According to node-on-android creator (@mafintosh) this is easier and better than J2V8 as it compiles V8 directly as the real thing.

Features:

  • Build full-fledged NodeJS applications, including UI (via native WebView)

Characteristics:

  • Relevant directories / files in gradle app project:
    • app/src/main/include/node with node .h headers
    • app/src/main/jniLibs/arm64-v8a with libc++_shared.so and libnode.so
    • app/src/main/cpp with native-lib.cpp (includes node.h)
    • Java code, just spins up a Service with node running in a separate thread
  • Has no JNI for libnode.so, so private native void startNode(String... app); shows as error in IDE (but compiles)
  • The NodeJS project resides in android/app/src/main/assets/node
  • NodeJS code is transferred to temporary storage and executed from there
  • NodeJS app specifies views to load in WebView via exposed loadUrl function
    • Node service accessible via NPM package node-on-android

Pros:

  • Simple project, not much plumbing code
  • Comes with a recent v8.x Node version out-of-the-box
  • Simple HTML-based app UI programming (e.g. using choo)
  • Works out-of-the-box :)

Cons:

  • Very new project, only experimental code still
  • Comes just for arm64 architecture (full mobile support planned, or DIY build)
    • Note: 64-bit cannot be combined with React Native (no 64-bit support)!
  • No native UI possible (unless coding in Gradle/Java/XML)
  • No debugging support on Node app (AFAIK, but maybe you can attach to the WebView somehow)

3. Combining React Native with NodeJS app-as-a-service (react-native-node)

Run a real Node.js process in the background, behind a React Native app.

Using this package you can: run http servers in Android, use Node streams, interface with the filesystem, offload some heavy processing out of the JS thread in React Native, and more! Running the real Node.js in Android, you can do everything that Node.js on desktop can.

Features:

  • Use React Native for the UI, NodeJS as a background service

Characteristics:

  • Derived from NodeBase
  • Very similar to node-on-android (run Service with Node on separate thread)
    • But node is compiled/used as application, not an embedded shared lib
    • NodeJS app code is located in {projectRoot}/background
    • NodeJS executable is in /android/src/main/res/raw/bin_node_v710
    • At build time Node app is tarballed, unpacked at `/android/src/main/res/raw/{appName}
    • NodeJS service is invoked as if run from the command-line, passing args
  • Node service RNNode is available in RN by importing react-native-node
    • react-native-node also contains CLI that transfers Node code at build time
  • The Example project communicates from React Native to NodeJS service via REST
    • Running an express server on http://localhost:5000 at Node side

Pros:

  • Simple project, not much plumbing code
  • Obvious: React Native support with NodeJS on android!
  • Node-as-executable will probably work with 64-bit devices + react-native

Cons:

  • Very new project, only experimental code still
  • Comes with old NodeJS 7.1.0 version (but DIY build newer ones)
  • No easy way to communicate between RN and Node apps (REST-based)
    • Need to extend REST API or roll your own mechanism
  • No debugging support on Node app. Really hard to know what's going on

Status (2017-08-17)

My goal is React Native + NodeJS. This is the status of my activities:

  • Compiling NodeJS v7.x versions as executable works
  • Compiling NodeJS v7.4.0 up to v7.9.0 works with new J2V8 build system
  • Compiling NodeJS v8.1.2 will soon work with J2v8 (compiled against libc++)
  • react-native-node does compile, but does not operate despite many tries
  • node-on-android works, but node-only app development and 64-bit incompatible with RN

I decided to combine react-native-node with J2V8 because of:

  • Great cross-compile build PR: https://github.com/eclipsesource/J2V8/pull/327
  • Builds into a nice J2V8 .aar to be easily included in Gradle

React Native 0.46.4 + NodeJS 7.9.0 is now working! See:

  • https://github.com/staltz/react-native-node/issues/5#issuecomment-323049897

My use case: fat client with P2P decentralized networking

I am thinking of a CQRS (command-query-responsibility-segregation) design:

  • react-native UI is constructed from view queried from the node service
  • react-native UI actions trigger commands on the node background service
  • background service processes network messages, incoming commands, triggers events
  • events are stored in Realm DB that forms the bridge between front and back

Details: Realm.io to bridge native NodeJS + React Native in Android fat client app (CQRS-style)


Conclusion

Even after years of people trying to port NodeJS to Android there are still no real good solutions, it is pioneering.

Expect many hurdles and errors as you set up your project and build environment, but once setup you could enjoy the full power of Node on your phone.

2 of 5
12

As of today (March 2018), there is another viable alternative not yet listed in the current answers: Node.js for Mobile Apps.

At its core, the project provides a native library for embedding Node.js into native Android and iOS applications; but it also comes with plugins for React Native and Cordova.

Pre-built binaries for the library are available for Android armeabi-v7a, x86, arm64-v8a, x86_64, and for iOS 64-bit.

The core library is a fork of nodejs/node-chakracore, which in turn is fork of nodejs/node. The Android version is pretty much regular Node.js built as a library, with a few portability fixes. The iOS version uses the ChakraCore engine instead of V8 (replacing V8 with ChakraCore is possible thanks to the changes in the nodejs/node-chakracore fork).

The React Native and Cordova plugins make it easier to add Node.js to applications built using those frameworks. The Node.js code runs in a separate engine and thread than the framework's (React Native / Cordova). Communication between the two JavaScript worlds is achieved via a messaging bridge provided by the plugins.

More information, including some documentation, is available on the project website.

(Full disclosure: I work for the company that develops Node.js for Mobile Apps.)

๐ŸŒ
DEV Community
dev.to โ€บ atordvairn โ€บ androidjs-build-android-apps-from-nodejs-1056
Android.js - build android apps from nodejs - DEV Community
January 7, 2022 - If You Can Build A Website, You Can Build An Android App! Android.js simple takes your node.js... Tagged with javascript, webdev, beginners, tutorial.
๐ŸŒ
Javascriptkicks
javascriptkicks.com โ€บ articles โ€บ 187438 โ€บ how-to-build-android-apps-with-node-js-using-android-js
How To Build Android Apps With Node JS Using Android JS | JavaScriptKicks
This is post about new Node JS framework for building android app, which provide Node JS runtime environment along with native environment in your app and lots of Native API's.
๐ŸŒ
Janeasystems
code.janeasystems.com โ€บ nodejs-mobile
Node.js for Mobile Apps
Node.js for Mobile Apps is a Node.js runtime that runs on Android and iOS, using the V8 JavaScript engine.
๐ŸŒ
Janeasystems
code.janeasystems.com โ€บ nodejs-mobile โ€บ getting-started-android
Getting Started - Node.js for Mobile Apps
The following steps will guide you through creating an Android Studio project that uses the library and is built with Gradle. The complete project can also be downloaded from the samples repo. This sample runs the Node.js engine in a background thread to start an HTTP server on port 3000 and return the process.versions value.
๐ŸŒ
GitHub
github.com โ€บ janeasystems โ€บ nodejs-mobile
GitHub - JaneaSystems/nodejs-mobile: Full-fledged Node.js on Android and iOS
This is the main repository for Node.js for Mobile Apps, a toolkit for integrating Node.js into mobile applications.
Starred by 2.6K users
Forked by 184 users
Languages ย  JavaScript 55.3% | C++ 27.1% | Python 12.5% | C 2.9% | HTML 0.9% | Shell 0.3%
๐ŸŒ
Nodejs-mobile
nodejs-mobile.github.io โ€บ getting started
Getting started | Node.js Mobile
The zip file contains Android binaries for the armabi-v7a, x86 (discontinued), arm64-v8a and x86_64 architectures. The following steps will guide you through creating an Android Studio project that uses the library and is built with Gradle. The complete project can also be downloaded from the samples repo. This sample runs the Node.js ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 39888563 โ€บ build-android-app-with-node-js
Build Android App with Node.js
How will I do it then in building my mobile App (Android APK)? ... I have my html5 files on the client folder... ... Yes sir. i am serving it in node.js ... You will need a server which can serve your files.. ... Node.js can only act as a server ( back-end ) of your app . You could use your HTML file and convert it to an apk with a compiler , but it's not worth the trouble.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ best android javascript compiler
r/learnprogramming on Reddit: Best android javascript compiler
July 30, 2024 -

Cant use my pc for a few weeks and I want to learn javascript. Im using youtube and mimo to learn but i wanna make personal projects to help me learn too. What are your recommendations? Bonus if theres webdev support or soemthing with html and css (idk if thats how it works idk enough abt webdev yet)

๐ŸŒ
Jscrambler
blog.jscrambler.com โ€บ native-node-js-for-mobile
Native Node.js for Mobile: Improve Your Development Cycles
The Android version of the core library uses the V8 JavaScript engine โ€“ much like regular Node.js. Since V8 doesnโ€™t run on iOS due to the OS restricting Just-in-time compilation, Janea Systems substituted their own port of the ChakraCore engine, integrated using what Microsoft created in ...