How to Install NodeJS With Express on a Raspberry Pi

Master Node.js and Express on your Raspberry Pi with step-by-step setup, package installation, and persistent deployment so you can start serving apps immediately.

You’ll set up NodeJS with Express on a Raspberry Pi for Node.js development, get a compatible Node build, and scaffold an Express app so your Pi can serve APIs or web pages. I’ll show the precise commands to update the OS, add the NodeSource repo, install node/npm, create a project, install Express, and run it persistently with pm2 or systemd—so you can move from zero to a reliable server quickly and without guesswork.

Key Takeaways

  • Update the Pi, check CPU with uname -m, then add NodeSource repo matching your ARM architecture before installing Node.js.
  • Install Node.js and npm (or use nvm for multiple versions), verify with node -v and npm -v.
  • Initialize your project with npm init, install Express via npm install express –save, and create app.js or server.js entry point.
  • Use dotenv and .gitignore, organize src/ with routes/controllers, and test endpoints with curl or browser on the configured PORT.
  • Run in production with pm2 (npm install -g pm2), pm2 start app.js, pm2 save, and enable pm2 startup for boot persistence.

Preparing Your Raspberry Pi for Development

Get your Raspberry Pi ready by gathering the right hardware, installing a supported OS, and configuring network and security settings before you start coding.

Get your Raspberry Pi ready: assemble the right hardware, install a supported OS, and secure your network before coding.

You’ll need the Pi board, correct power supply (match your model), a 16–32GB microSD (32GB recommended), keyboard, mouse and a display or prepare for headless operation.

Flash Raspberry Pi OS or Ubuntu Server to the SD after formatting FAT. Be sure to use the correct power rating for your model, for example a 5V/3A supply for Raspberry Pi 4.

Use raspi-config to expand filesystem, change default passwords, and enable SSH.

Prefer Ethernet for stable development, or preconfigure Wi‑Fi for headless setups.

Install git, build-essential and your preferred editors; use VS Code over SSH for efficient workflow and keyboard shortcuts.

Define backup strategies for your SD image and project dirs to protect innovation.

Also make sure to have a reliable 2A micro USB power supply for optimal performance.

I recommend running the same server OS as production, such as Ubuntu Server, so your development environment closely matches deployment.

Installing Node.js on Raspberry Pi

Before you install Node.js, confirm your Pi’s CPU architecture and pick the right distribution—NodeSource provides up-to-date binary packages for ARMv7+ devices (Pi 2/3/4 and Zero 2), while older ARMv6 models need alternate binaries or source builds. On newer Pi models, upgraded CPUs like the Cortex-A76 deliver significantly better single-core performance which can speed up Node.js builds and runtime.

Add the NodeSource repo for your desired NODE_MAJOR (example: 20 or 22), run sudo apt update, then sudo apt install nodejs.

Verify with node -v. Using NodeSource keeps updates simple and avoids slow source builds.

  1. Choose LTS for stability or current for cutting-edge features.
  2. Use NodeSource binaries on ARMv7+; avoid ARMv6 pitfalls unless you require special handling.
  3. If needed, consider cross compilation techniques or manual builds only for unique hardware constraints.

You should also ensure your Raspberry Pi has a reliable power supply and a class 10 microSD card to prevent installation issues and improve performance power and storage. Additionally, make sure you update the system and install essential build tools like build-essential before compiling native modules.

Installing and Managing Npm

manage packages for nodejs with Express on a Raspberry Pi efficiently

Install NPM with apt (sudo apt install npm) or use nvm if you need multiple Node/NPM versions, and you’ll get the CLI tools to add packages for GPIO and other Pi-specific work. Raspberry Pi OS provides optimized GPIO support and a lightweight desktop environment that helps Node.js projects run efficiently on Pi hardware.

Keep dependencies tidy by using package.json and semantic versioning, then use npm install, npm update, and npm uninstall to control versions and avoid conflicts.

Use npm scripts to automate builds and common tasks so updates and package management stay consistent across your projects.

Node.js on Raspberry Pi is lightweight and well-suited for low-powered hardware.

You can get NPM by installing Node.js from the official website or package manager, since NPM is included with Node.js and provides a command-line client for package management and publishing CLI tools.

Installing Npm Efficiently

Efficiently managing npm on a Raspberry Pi means choosing the right installation method, minimizing unnecessary installs, and keeping dependencies predictable; you’ll typically stick with apt for simple setups, NodeSource for current Node/npm releases, and global installs only for cross-project tools like PM2. For headless setups, enable SSH remote access to install and manage npm from another machine. Use apt when you want stability and minimal overhead; use NodeSource for newer features and npm versions. Prefer lightweight registries and atomic installs where possible to reduce bandwidth and guarantee consistent rollbacks.

  1. Verify: run npm –version and prefer NodeSource for newer ARM builds.
  2. Global vs local: install CLIs globally (PM2), keep libraries per-project in package.json.
  3. Optimize: use –production, prune unused packages, and clean npm cache. Follow audits and avoid root installs unless required.

Additionally, for reliable runtime management on Raspberry Pi, consider using PM2 process manager to keep Node.js services running and auto-started. You should also confirm your system has Node.js and npm installed before starting.

Updating and Managing Packages

Now that you’ve got npm set up and decided when to use global versus local installs, managing and updating those packages keeps your projects stable and secure. Also ensure your Raspberry Pi has a stable 5V supply to avoid undervoltage during heavy package builds.

You should run npm update in projects to refresh compatible dependencies, and npm update -g for global tools like PM2.

Regular dependency auditing with npm audit catches vulnerabilities early; fix issues with npm audit fix or targeted upgrades.

Keep system packages current too: sudo apt-get update then sudo apt-get upgrade before installing system-level tools.

Use npm uninstall or apt-get –purge remove to remove unwanted components and preserve package hygiene.

For Python needs, leverage pip3 and virtual environments to avoid cross-contamination.

Back up configs, use PM2 to manage production processes, and plan rollbacks before major upgrades.

Also remember that the Raspberry Pi uses a system-level package manager (APT) for OS and binary dependencies, so check and install required system dependencies before compiling or running new software.

Node.js on Raspberry Pi runs on different ARM architectures, so confirm your board’s CPU with uname -m to download the matching ARM binary.

Creating an Express Project

When you start an Express project on your Raspberry Pi, initialize it with npm to create a package.json that will track dependencies, scripts, and metadata; then install express (and any other libs) with npm install –save so your setup is reproducible across machines. You can schedule routine maintenance tasks with cron to keep the server healthy.

Create a src-based project scaffolding: config, controllers, routes, middlewares, services, utils.

Add .gitignore and a .env early to manage environment variables securely.

  1. Initialize: npm init, lowercase filenames, camelCase variables for consistency.
  2. Structure: group routes by feature, keep controllers separate, store config in config/.
  3. Middleware & utils: centralize reusable middleware and helpers for scalability.

Commit the manifest and ignore node_modules; collaborators will reproduce the environment with npm install. Additionally, keep sensitive settings out of source control by loading them from a centralized config using dotenv so environments remain secure and consistent. Also, organize core files like server.js and app.js in a clear entry-point structure to support scalability.

Running and Accessing the Express Server

start a server with nodejs with express on a raspberry pi: enable ssh

Start the server from your Express project folder with npm start (or node app.js if you customized the entry) and make sure your Node and npm versions match Express requirements. If you need secure remote control, enable SSH key authentication to manage the Pi without exposing passwords.

Confirm network accessibility by testing http://localhost:3000 on the Pi or http://:3000 from another device, and adjust firewall or router settings if you need external access.

Test your endpoints in a browser or with curl/postman and monitor the terminal or logs for errors so you can iterate quickly. Also remember that Raspberry Pi Model B+ has limited RAM and CPU, so installs and builds can be slower and may require swap or lighter alternatives to avoid failures (limited RAM and CPU).

For reliable long-term hosting, consider setting up a process manager like systemd service or pm2 to keep the server running and restart it after crashes or reboots.

Starting the Server

Once you’ve navigated to your app directory, launch the Express server with npm start or node app.js and confirm the console shows it’s listening (default port 3000 unless you’ve changed process.env.PORT). You can also use the Raspberry Pi Imager’s pre-boot configuration features to set up SSH, Wi‑Fi, and hostnames before first boot, which simplifies headless deployment.

You should see the Express welcome page at http://localhost:3000 on the Pi. For continuous operation use a process manager (pm2) to enable auto restart and basic log rotation.

  1. Install and start: npm install pm2 -g; pm2 start app.js; pm2 save.
  2. Boot persistence: run pm2 startup and follow its instructions, or add a su pi -c ‘node /home/pi/yourapp/server.js < /dev/null &’ line in /etc/rc.local before exit 0.
  3. Monitor and debug: use pm2 logs, top/htop, and check for port conflicts; restart gracefully when needed.

I originally built this setup to run a custom NodeJS server on a Raspberry Pi as a lightweight development and sharing environment, using the Pi to host React apps without third-party hosts like Heroku, which makes the Pi a handy development server. It’s important to ensure you have Node.js installed on the Pi before beginning setup.

Network Accessibility

To make your Express app reachable from other devices, make sure the Pi is on a reliable network, has a fixed internal IP, and the server is listening on the intended port — then test access using that IP and port (for example, http://192.168.1.100:3000). Using Raspberry Pi OS can improve hardware compatibility and display performance.

Use nmcli to join Wi‑Fi and confirm connectivity with nmcli dev wifi list.

Set a static IP via /etc/dhcpcd.conf or DHCP reservation in the router, then verify with sudo ip addr show.

Add port forwarding in your router to map the external port to the Pi’s internal IP and TCP port; restrict forwarding to necessary ports only.

Harden access with ufw, keep software updated, and prefer SSH keys.

For advanced deployments consider VPN setup and plan IPv6 configuration to future‑proof external reachability and routing.

Also consider expanding the filesystem immediately after first boot so the OS can use the full storage capacity and avoid running out of space (expand filesystem).

You can also turn the Raspberry Pi into a simple network gateway using an extra NIC or USB adapter to separate WAN and LAN traffic for improved isolation and routing (network interfaces).

Testing Endpoints

Having confirmed network access and port mappings, you’ll focus next on running the Express server and verifying its endpoints from both the Pi and remote machines. For optimal performance consider using Raspberry Pi OS which offers full hardware compatibility and access to thousands of packages.

Start the app with node server.js (or npm script) and watch the console log for the URL and port. From the Pi use curl, locally test GET /hello and POST /calculate with JSON; from another machine target http://PI_IP:3000. For repeatable validation, integrate automated validation and endpoint mocking into your workflow. The Express app should use a JSON body parser middleware like body-parser to correctly parse POST request bodies. Additionally, export your Express app without calling listen so you can test it directly with Supertest.

  1. Export app without app.listen(), use Supertest to simulate .get()/.post().
  2. Pair Supertest with Jest or Mocha, run async tests, assert status, headers, body.
  3. Isolate tests, mock dependencies, reset state to avoid side effects. These steps modernize testing and scale reliably.

Best Practices and Troubleshooting

secure lean managed node js

While you can get Node.js installed several ways, following best practices and knowing common fixes will save you time and keep your Express app reliable on a Raspberry Pi. Prioritize security hardening and dependency auditing: use NodeSource for current ARM builds, run apt update/dist-upgrade first, and keep npm packages trimmed and audited (npm audit). Initialize projects with npm init and save Express in package.json to enable reproducible installs.

Run apps with a lightweight process manager like pm2, assign static IPs, and expose only needed ports. Fix common issues by symlinking node if missing, avoiding global npm installs or using nvm, and reinstalling node_modules when modules fail. Monitor CPU/memory; optimize code or offload heavy tasks to preserve availability.

Frequently Asked Questions

Can I Run Multiple Express Apps on Different Ports Simultaneously on a Raspberry Pi?

Absolutely — you can. You’ll happily run multiple instances on different ports; just avoid port conflicts, use PM2 or a proxy for scaling and reliability, and make certain firewall rules and unique app.listen(port) values.

Can I Use PM2 or Systemd to Auto-Restart My App?

Absolutely — you can; it’s insanely reliable: you’ll get process monitoring and graceful restarts with PM2 for Node-centric convenience, or systemd for lean OS-level control; choose based on resources, integration needs, and operational scale.

Will Node.Js Work on Older Raspberry Pi Models?

Yes — but ARM compatibility varies: older ARMv6 Pis need source builds or outdated packages. You’ll face CPU limitations, possible thermal throttling and higher power requirements, so expect slow builds and constrained runtime performance.

How Do I Deploy Updates Without Downtime for NodeJS with Express On a Raspberry Pi?

You deploy updates with zero downtime by using blue green deployments, PM2 or clustering, graceful shutdowns, a load balancer (Nginx), CI/CD automation, health checks, and rollback/versioning to switch traffic seamlessly and safely.

Conclusion

You’ve prepared the Pi, installed Node.js, and set up npm; you’ve scaffolded an Express app, started it, and configured it to run reliably. Keep your system updated, use the correct Node binary for your CPU, test endpoints locally, and monitor with pm2 or systemd. Back up code, secure ports, and rotate logs. Repeat deployments with npm scripts, repeat restores with backups, and repeat improvements with testing and incremental upgrades.