Embedded Linux systems often require a lightweight JavaScript runtime environment that supports Node.js or pure JavaScript engines (such as Duktape or QuickJS). The following steps outline the setup method and example tests.
Environment Selection and Installation
Depending on the resource limitations of the embedded device, the following options can be selected:
Node.js Option: Suitable for devices with sufficient resources (such as Raspberry Pi). Download the ARM architecture Node.js binary package (for example, <span>node-v25.2.0-linux-arm64.tar.gz</span>, which seems to only be available for arm64; other ARM architectures need to be compiled from source). After downloading, extract and configure the environment variables:
wget https://nodejs.org/dist/latest/node-v25.2.0-linux-arm64.tar.gz
tar -xvzf node-v25.2.0-linux-arm64.tar.gz
export PATH=$PATH:/path/to/node-v25.2.0-linux-arm64/bin
QuickJS Option: Suitable for resource-constrained devices. Download and compile the QuickJS engine:
git clone https://github.com/bellard/quickjs.git
cd quickjs && make
export PATH=$PATH:$(pwd)
Example Code Testing
Node.js Test Create a <span>test.js</span> file with the following content:
console.log("Hello, Embedded Linux!");
setTimeout(() => {
console.log("Delayed log after 1 second");
}, 1000);
Run command:
node test.js
QuickJS Test Create a <span>test_quickjs.js</span> file with the following content:
print("QuickJS Running on Embedded Linux");
Run command:
qjs test_quickjs.js
HTTP Server Example Create a simple HTTP server:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from Node.js server!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
File System Operation Example Example of reading and writing files:
const fs = require('fs');
// Write to file
fs.writeFile('example.txt', 'Hello Node.js!', (err) => {
if (err) throw err;
console.log('File written successfully');
// Read from file
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log('File content:', data);
});
});
Using npm to Install Modules Install commonly used modules (such as Express):
npm install express
Express Framework Example Create an Express application:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Welcome to Express!');
});
app.listen(3000, () => {
console.log('Express app listening on port 3000');
});