20. Setting up Node.js (NPM) Environment
Termux is an excellent environment for running JavaScript backends and managing front-end tooling using Node.js and NPM (Node Package Manager).
1. Installation
The nodejs package installs both Node.js and NPM.
bash $ pkg install nodejs
Verify the installation:
bash $ node -v v18.X.X $ npm -v 9.X.X
2. Running a JavaScript File
Create a simple Node.js file:
bash $ nano app.js
Content of app.js:
javascript console.log('Node.js is running successfully in Termux!');
const http = require('http');
http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello from Node on Termux!\n'); }).listen(3000);
console.log('Server running at http://localhost:3000/');
Execution:
bash $ node app.js Node.js is running successfully in Termux! Server running at http://localhost:3000/
(Note: This server will only be accessible locally on your device or via Termux networking tools, which we cover later.)
3. Installing Node Packages
Use npm to install dependencies for your projects, such as web frameworks like Express.
bash $ npm install express
By having Python and Node.js set up, your Termux environment is ready for serious mobile development.