NodeJS on Android [Root]
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.
Pushing the Binary
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"
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”
adb shellecho "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.
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.
Installing Node 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.
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.
Pushing the Files
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!
Thanks for taking the time to read this, I hope you found it helpful. If you did please consider applauding, sharing or following for more content. If you have any questions or comments don’t hesitate to reach out on LinkedIn.