Skip to content

Setup Guide: Raspberry Pi | Install Node.js via Node Version Manager (NVM)

rourke edited this page Jan 15, 2018 · 6 revisions

Install Install Node.js via Node Version Manager (NVM)

Install Node Version Manager (NVM)

# Install Node Version Manager (NVM)
curl -o- | bash

# Rerun Profile script to start NVM
source ~/.bashrc  # Rerun profile after installing nvm

Install Node.js via NVM

Installing an ARM-version of Node has become very easy:

# Install Node.js using Node Version Manager
nvm install 8  # Installs Node v8, (nvm install stable) installs Latest version of node
nvm use 8  # Sets Node to use v8

Update Node Package Manager (NPM)

npm install npm@latest -g

To make sure it ran correctly, run npm -v, nvm --version, & node -v. It should return the current versions.

# Output Node Related Version Info
echo "[NPM] ============"; which npm; npm -v;
echo "[NVM] ============"; nvm --version; nvm ls
echo "[NODE] ============"; which node; node -v

### Install Node.JS Serial Port application first (OPTIONAL) npm install serialport

Install CNCjs

# Install Latest Release Version of CNCjs
npm install -g cncjs@latest --unsafe-perm

# --- OR ---

# Install Specific Version of CNCjs
#npm install -g cncjs@v1.9.0-beta.1 --unsafe-perm  # Installs Specific Version based on TAG

Allow access to port 8000 from port 80

# Allow access to port 8000 from port 80
sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

# Make Iptables Persistent
sudo apt-get install iptables-persistent -y

# How-to: Save & Reload Rules
#sudo netfilter-persistent save
#sudo netfilter-persistent reload

# How-to: Manually Save Rules
#sudo sh -c "iptables-save > /etc/iptables/rules.v4"
#sudo sh -c "ip6tables-save > /etc/iptables/rules.v6"

# Run this if issues to reconfigure iptables-persistent
# sudo dpkg-reconfigure iptables-persistent

Auto Start Options

Install Production Process Manager [PM2]

# Install Production Process Manager [PM2]
npm install pm2 -g

# Start CNCjs (on port 8000) with PM2
pm2 start $(which cncjs) -- --port 8000

# Setup PM2 Startup Script
pm2 startup debian
#[PM2] You have to run this command as root. Execute the following command:
#sudo su -c "env PATH=$PATH:/home/pi/.nvm/versions/node/v{NODE-VERSION}/bin pm2 startup debian -u pi --hp /home/pi"
sudo su -c "env PATH=$PATH:/home/pi/.nvm/versions/node/v6.5.0/bin pm2 startup debian -u pi --hp /home/pi"

# Set current running apps to startup
pm2 save

# Get list of PM2 processes
pm2 list

Auto Start on Boot with cron (Alternative)

# Get Node Location (take note of this)
which node

# Open crontab
crontab -u pi -e

# Paste the following into Cron Tab [ NOTE: which cnc  # used to find location of application ]
@reboot env PATH=$PATH:/home/pi/.nvm/versions/node/v8.0.0/bin /home/pi/.nvm/versions/node/v8.0.0/bin/cnc >> $HOME/cncjs.log 2>&1
Clone this wiki locally
You can’t perform that action at this time.