Search by Tags

Node.js in Linux


Article updated at 29 Jan 2020
Subscribe for this article updates

Node.js Setup

With an Existing Linux Image

Node.js can be installed to an existing Linux image using the package manager (opkg). To see a list of relevant packages, enter the following commands:

opkg update
opkg list | grep nodejs

Node.js may be installed using the following command:

opkg install nodejs

As an alternative, you can install a specific Node.js version from the Node releases repository. Notice that NPM will also be installed.

As an example, the v6.2.0* version was chosen:

cd ~
tar zxvf node-v6.2.0-linux-armv7l.tar.gz && rm node-v6.2.0-linux-armv7l.tar.gz

You must add the Node.js binary folder to the PATH persistenty. One way to do it is by adding the following line to ~/.profile:

export PATH=$PATH:/home/root/node-v6.2.0-linux-armv7l/bin

Or install to the /usr/local folder and remove the installation files:

cp -av ./node-v6.2.0-linux-armv7l/bin/. /usr/local/bin
cp -av ./node-v6.2.0-linux-armv7l/include/. /usr/local/include
cp -av ./node-v6.2.0-linux-armv7l/lib/. /usr/local/lib
cp -av ./node-v6.2.0-linux-armv7l/share/. /usr/local/share
rm -r ./node-v6.2.0-linux-armv7l/

NPM - Node Package Manager

To install NPM using opkg:

opkg install nodejs-npm

To update NPM:

npm install npm -g

While Building a Linux Image

Node.js can be added to an OpenEmbedded Linux build for inclusion into the image's rootfs. After establishing the image build configuration, additionally append the following line to the oe-core/build/conf/local.conf file:

IMAGE_INSTALL_append = "nodejs"

Then execute (or re-execute) 'bitbake' for the image (ie. 'bitbake angstrom-lxde-image'). For more information about building a Linux image, see OpenEmbedded (core).

Performance & Suitability

Since Node.js is a server-side interpreter for the Javascript language, it is expected to perform slower than a compiled language such as C/C++. However, because of its event-driven paradigm, it may well suit web I/O intensive tasks, such as implementing a web server.

Still, Node.js can also be viable for tasks that aren't very CPU intensive, which may be a fast development solution since Javascript is a high level programming language.


If you want to run Node-RED on Linux, we show how to run it on Torizon:


GPIO - Blinking LED

var fs =  require('fs');
var export_path = '/sys/class/gpio/export';
var blink_period = 500; //in milisseconds                    
var gpio_53 = new NewGpio(53, 'out', 0);                                             
function NewGpio (id, direction, state) {                           
        this.number = id;                                                     
        this.path = '/sys/class/gpio/gpio' + id + '/';                        
        this.direction = direction;                                 
        this.value = state;                                                   
function export_pin (pin, callback){                                        
//callback has error as argument, or null if no error    
        fs.stat(pin.path, function(err, stats) {                        
                if (!err) callback(null, pin);//if gpio already exported      
                else{//if gpio not exported yet                               
                        //export it                                           
                        fs.writeFile(export_path, pin.number, function (err) {
                                if (err) callback(err, null);
                                else callback(null, pin);
function configure_pin(pin, callback){                   
//callback has error as argument, or null if no error                   
        fs.writeFile(pin.path + 'direction', pin.direction, function (err) {  
                if (err) callback(err, null);                                 
                else callback(null, pin);                           
function blink(pin){                                                        
        fs.writeFile(pin.path + 'value', pin.value, function (err) {    
                if (err) throw err;                                           
                pin.value ? pin.value = 0 : pin.value = 1;              
//here the code execution starts                                            
export_pin( gpio_53, function (err, pin) {                                  
        if (err) throw err;                                             
        configure_pin(pin, function (err, pin) {         
                if (err) throw err;                                     
                }, blink_period);//call blink periodically