Install Node.js and NPM on Raspberry Pi

[1: Recommended] for 64-bit only. Raspberry OS 32-bit is already coming with Node-Red pre-installed

you will get:

  1. node.js and npm installed (LTS version)
  2. pigpio module
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

source: Running on Raspberry Pi : Node-RED


[2: Good]

To install node.js and npm without installing node-red or to update installed at step 1:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs

source: Beginner’s Guide to Installing Node.js on a Raspberry Pi

Good: this method could be used to fix most of problem with node+npm

Bad: Failed to install node-red if I’d have latest version installed


[3: Not recommended]

sudo apt -y install nodejs
sudo apt -y install npm

i’ve had a problem with npm (something like: “does not support node version installed”). So I  had to go back to [2]

[4: Test installation]

create a file (for example: nano test.js):

const http = require('http');


http.createServer(function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8080/');

and run from terminal

node test

start any browser and enter we address

http://localhost:8080

if you see ‘hellow world’, everything works!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Yandex.Metrica