Search by Tags

Node.js in Linux

 

Article updated at 03 May 2022
Compare with Revision


Node.js Setup

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"

You may also include NPM:

IMAGE_INSTALL_append = "nodejs nodejs-npm"

You may find the page TipsAndTricks/NPM from the Yocto Project Wiki useful.

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.

Node-RED

Torizon

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

Linux BSPs

Install Node-RED with the following command:

# npm install -g node-red --unsafe-perm

Start Node-RED:

# node-red

Examples

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    
        console.log('first');                   
        console.log(pin);                                               
        fs.stat(pin.path, function(err, stats) {                        
                console.log('second_not_exp');                          
                console.log(pin);                                             
                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) {
                                console.log('second_exp');                  
                                console.log(pin);            
                                if (err) callback(err, null);
                                else callback(null, pin);
                        });                     
                }                                           
        });                                                         
}  
 
function configure_pin(pin, callback){                   
//callback has error as argument, or null if no error                   
        console.log('third');                                           
        console.log(pin);                                           
        fs.writeFile(pin.path + 'direction', pin.direction, function (err) {  
                if (err) callback(err, null);                                 
                else callback(null, pin);                           
        });                                                                   
}  
 
function blink(pin){                                                        
        console.log('fourth');                                          
        console.log(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;                                     
                setInterval(function(){                                       
                        blink(pin);                                     
                }, blink_period);//call blink periodically                    
        });                                                                   
});