Installing and using Node.js and Angular on a very resource-constrained microcomputer such as the Raspberry Pi Zero 2 W requires optimizing memory usage so that the build/deploy chain does not cause the OS to kill processes that tend to exhaust available resources.
The following describes the steps required to install and successfully use both Node.js and Angular to build viable applications on a Raspberry Pi Zero 2 W having only 512MB RAM. The AngularTourOfHeroes tutorial below was completed successfully using the Raspberry Pi Zero 2 W with the OS configured as given below.
Target hardware: Raspberry Pi Zero 2 W
Target OS: Raspbian GNU/Linux 11 (bullseye)
Angular CLI: 15.1.4
Node: 18.14.0
Package Manager: npm 9.4.1
Angular: 15.1.3
Note that you must use an even numbered version (e.g., 18.x LTS) of Node.js when building with Angular.
We're assuming a freshly built Raspberry Pi OS image, just booted for the first time.
sudo apt -y update
sudo apt -y upgrade
sudo apt -y install build-essential
sudo apt -y install git
git config --global user.email "your.email@address"
git config --global user.name "Firstname Lastname"
sudo dphys-swapfile swapoff
sudo nano /etc/dphys-swapfile
- Find the 'CONF_SWAPSIZE' line
- Change it to read: 'CONF_SWAPSIZE=2048'
sudo dphys-swapfile setup
sudo dphys-swapfile swapon
sudo swapon --show
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install npm@latest -g
sudo npm install tar@latest -g
export NODE_OPTIONS=--max-old-space-size=512
You should add the export
command to your .bashrc
file so that it is set during each session.
sudo npm install @angular/cli -g
ng version
ng serve --host 192.168.1.50 --port 4000
See also: https://angular.io/cli/serve
Your Raspberry Pi should now be able to build and deploy an Angular application (on a test server). You should also be able to fully execute the tutorial below.
This project was generated with Angular CLI version 15.1.4.
Run ng serve
for a dev server. Navigate to https://localhost:4200/
. The application will automatically reload if you change any of the source files.
Run ng generate component component-name
to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module
.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory.
Run ng test
to execute the unit tests via Karma.
Run ng e2e
to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI Overview and Command Reference page.