NodeJS on Android [Root]

If you’ve ever wanted to run a Node server or Javascript on Android this is the article for you!

Aaron Watson
Aug 1 · 5 min read

I created this guide to explain some things about running Node on Android. We will go over how to get up and running with Javascript and Node on Android. When I originally tried to find information on this I found it hard to come up with anything solid so I thought I would share my findings here!

Why Would You Want to Run Node / Javascript on Android?

The reason I originally looked into this was that I had a WebRTC application that needed a signaling server and I wanted to run it on the embedded system so I wouldn’t need a second computer. Another use case might be that you have a bunch of code written in Javascript and you want to leverage that on Android. Adding Node to the embedded Android toolkit is a huge bonus as well since there are a lot of problems that can easily be solved using it.

Download Node

The first thing that will be required to run any Javascript natively on Android will be Node, the way we can get Node for Android is to download a pre-compiled binary. You will need one that will run on the CPU architecture that your android device has. In my case, I will be using a Snapdragon 820 dev kit so I’ll be getting an arm64 binary. If you are unsure of what version you need I would start with arm and if that doesn’t work try x86. You can download the binary you need from GitHub here. I recommend just downloading the single file from the browser since it is a lot of precompiled binaries to clone the entire repo.

Install the Binary on the Android Device

To use the Node binary, we will need to push it to our device and add execute permissions. The best way to do this is over ADB, if you are unfamiliar with ADB it is beyond the scope of this tutorial however a great place to find information on it would be the documentation here.

Once you download the appropriate binary for your device it’s time to push it to the device. For the sake of this guide, I will be installing it in the system partition so it will directly accessible from the shell. However you can place it wherever you like but you will need to call it with a fully qualified path.

We need to remount /system as r/w so we can push the binary, you can achieve this with the following command.

adb remount

Next, we need to push the binary to the device, you will need to change the source folder to point to the location that you downloaded the Node binary earlier.

adb push “E:\sourcetree\nodejs-android-prebuilt-binaries\nodejs-7.8.0-android-arm64-full\bin\node” /system/bin

Finally, to use it we will need to change the permissions to allow execution.

adb shell “chmod +x /system/bin/node”

Hello World from Javascript

To test the Node binary we will do a simple version check in the system, run the following command, if it doesn’t return a version number either you have the wrong binary or something else is wrong.

adb shell “node -v”

For fun let’s create a simple hello world in javascript and execute it on the system. You will need to have a folder on the device that you are using to store the Javascript files, for the remainder of the tutorial I will be using /data/local/tmp however you can use whatever folder you want.

You can create a hello world in Javascript in just one line so let’s just create it using ADB shell. First we’ll need to enter the shell and then call the echo command once we’re in (You should see a # or $ at the prompt).

adb shell
echo “console.log(‘Hello Android!’);” > /data/local/tmp/hello.android.js

To execute the file simply call Node the same way you would on a Linux computer. Also if you installed Node to a different location don’t forget to call it using a fully qualified path.

node /data/local/tmp/hello.android.js

If everything went correctly you should see the text “Hello Android!” in the output! Great, let’s take it to the next level.

Launching a Full Express Server on Android

Let’s launch an Express server on our device, for this part of the guide we will need the files for the express server. I’ll be using ProjectRTC which is a WebRTC signaling server built on Node using Express. You can download the source here.

You will need to download the Node packages locally so you will need to have NPM installed on your computer since the Android device doesn’t have NPM and can’t download the required packages.

To install the Node packages you will need to cd into the folder that contains the package.json file in the root of the project and then execute the following command.

npm i

Once that command completes you should see a node_modules folder with thousands of files in it. Now we need to push all the contents of the root project folder to the device, again I’ll be using /data/local/tmp but feel free to place it anywhere you like.

My project folder is located at E:\sourcetree\ProjectRTC however you will need to change this to point to your folder, also this might take a while so feel free to get a snack while it copies.

adb push “E:\sourcetree\ProjectRTC” “/data/local/tmp”

And you can launch it with

adb shell “node /data/local/tmp/ProjectRTC/app.js”

If you see the text “Express server listening on port 3000” it means it’s working!

Awesome!

There you have it, you just launched Javascript code on an Android device. Now you can turn your phone into a fully-fledged webserver if you like! (Although I wouldn’t recommend it)

Thanks for taking the time to read this, I hope you found it helpful. If you did please consider applauding, sharing or subscribing for more content. If you have any questions or comments don’t hesitate to drop me a line.

If you enjoyed this guide you might like my similar guide Python on Android!

About the author

My name is Aaron Watson and I’m a Software Engineer specializing in embedded Android development based out of Clarenville, Canada. If you’d like to get in touch here is my LinkedIn, GitHub and my website.

Aaron Watson

Written by

I’m a Software Engineer specializing in embedded Android development based out of Clarenville, Canada

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade