Step-by-Step Node.js Setup for Raspberry Pi

Step-by-Step Node.js Setup for Raspberry Pi

1. Update packages on raspberry pi

sudo apt update
sudo apt upgrade


2. Install the packages we need to access the Nodesource repository.

sudo apt install -y ca-certificates curl gnupg


3. Set up the NodeSource repository.

Download the Nodesource GPG key and store it within the “/usr/share/keyrings” directory.

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/nodesource.gpg


4. decide what NodeJS version you want to install. Use version that is most recent.

NODE_MAJOR=22


5. Add the Node.JS repository to your Raspberry Pi’s sources list.

echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list


6. Because of the changes to your sources list you will need to run an update of the package list again.

sudo apt update


7. Installing Node.JS

sudo apt install nodejs


8. To verify that we have now successfully installed NodeJS

node -v


9. When using NPM to install additional modules you may have issues when the module needs compiling to support the native hardware.

We can install the “build-essential” package to solve most of these issues. This package contains various tools used to compile software.

sudo apt install build-essential


10. Check that Node.js and npm are installed. You can check with:

node -v
npm -v

If they are not installed, here’s a reliable way using NodeSource:

🔧 Install Node.js (Recommended LTS Version):

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

Then confirm:

node -v
npm -v


11. Install PM2 Globally

Once Node.js and npm are set up, install PM2 globally:

sudo npm install -g pm2

After installation, check if it works:

pm2 --version


12. Optional: Autostart on Boot

To make your app restart automatically on reboot:

pm2 startup

It will show a command — copy and run it (usually includes sudo env ... pm2 startup systemd ...).

Then save the current process list:

pm2 save

Follow the command it shows you and run it. It’ll look something like:

sudo env PATH=$PATH:/home/pi/.nvm/versions/node/v18.X.X/bin pm2 startup systemd -u pi --hp /home/pi

Then again:

pm2 save

🧭 Step-by-Step to Start Your App

1. Locate the App’s Entry File Most Node.js apps have a main entry file like:

    Most Node.js apps have a main entry file like:

    • index.js
    • app.js
    • server.js

    If you’re unsure, check the package.json file for the main field:

    cat package.json | grep main

    Example output:

    "main": "server.js"

    That means your app starts with server.js.

    2. Install Dependencies on the Raspberry Pi:

    • SSH into your Raspberry Pi.
    • Navigate to your application’s directory (e.g., cd wordace).
    • Run: npm install
    • This might take a bit longer on a Raspberry Pi compared to your PC due to its processing power.

    3. Build the Application for Production:

    In the same application directory on the Pi,

    npm run build 

    This will create the .next folder optimized for production. Again, this step can take some time on a Pi.

    Start your app with PM2:

    pm2 start npm --name wordace -- start

    Ensure it starts on boot:

    pm2 startup 

    (Follow the command PM2 gives you to set up the startup script).

    pm2 save

    8. Accessing Your App:

    • Once the app is running (either with npm start or PM2), you can access it from another device on the same network by going to: http://YOUR_RASPBERRY_PI_IP_ADDRESS:3000 
    • (Replace 3000 if your app or Next.js is configured to run on a different port).


    🔁 Here’s what you should do after code changes:

    Navigate to your project directory (if you’re not already there):
    cd /home/projects/nodeJS/WordAce

    Rebuild the app: 
    npm run build
    Restart the PM2 process: 
    pm2 restart wordace

    Comments

    No comments yet. Why don’t you start the discussion?

    Leave a Reply

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

    This site uses Akismet to reduce spam. Learn how your comment data is processed.