
Raspi Get Started in NodeJs
Getting started with Node.js on Raspberry Pi is an awesome way to build real-world projects using JavaScript β whether itβs blinking LEDs, reading sensors, or building IoT dashboards π
Hereβs a simple step-by-step guide to get up and running:
β 1. π¦ Install Node.js on Raspberry Pi
Option 1: Use APT (Recommended for most users)
sudo apt updatesudo apt install nodejs npm -y
Check installation:
node -vnpm -v
β 2. π§ͺ Test Node.js is Working
Create a test file:
nano test.js
Add this:
console.log("Hello from Raspberry Pi and Node.js!");
Run it:
node test.js
βοΈ You should see the message in your terminal.
β 3. π Create a Node.js Project
mkdir pi-projectcd pi-projectnpm init -y
This creates a package.json
file.
β 4. β‘ Control GPIO (e.g. LED Blinking)
Install the onoff
package:
npm install onoff
Example to blink an LED on GPIO17:
// blink.jsconst Gpio = require('onoff').Gpio;const led = new Gpio(17, 'out');setInterval(() => { led.writeSync(led.readSync() ^ 1); // Toggle LED}, 500);// Cleanupprocess.on('SIGINT', () => { led.writeSync(0); led.unexport(); console.log("Stopped."); process.exit();});
Run with:
sudo node blink.js
β 5. π Other Cool Things You Can Do
Feature | Library |
---|---|
Control LEDs/Buttons | onoff , rpi-gpio |
Use sensors (DHT11) | node-dht-sensor |
Create web servers | express |
Control from phone | socket.io , REST APIs |
Send data to cloud | axios , mqtt |
Use Pi Camera | raspistill , node-webcam |
β 6. π€ Example: Web Controlled LED
Want to control an LED using a browser button?
I can help you build that next β just ask!