Installing Node.js, Node-RED and PM2 offline
Intro
When using Node.js the easiest way to install everything is by installing the package from the PLCNext Store. From then on you can install all needed dependencies with the use of npm (Node Package Manager).
However, in some cases you won’t be able to connect the PLC to the store in order to get the software you wish to install. In what follows you will learn how to install Node.js, Node-RED and PM2 offline. Once installed you will be able to run locally installed packages on the controller.
All preparations that are needed in order to prepare all packages for offline installation are carried out on a Linux host.
Preparations on the Host computer with connection to the internet
Node.js
At the time of writing, the latest stable Node.js version was 10.16.3. If you want to check the latest release of Node.js, you can just visit the main page of Node.js (https://nodejs.org/en/). Once you know the release you want to install, execute following command (adapt your release version):
curl -LO https://nodejs.org/download/release/v10.16.3/node-v10.16.3-linux-armv7l.tar.gz
PM2
In contrary to Node.js, PM2 can’t be downloaded as a tar.gz. In order to install it we need to download its npm package and make a tarball ourselves. To do this, make sure you have Node.js and NPM installed on your host system.
To start off, execute the following command:
npm install –g npm-bundle
This installs the package “npm-bundle” globaly. The “npm-bundle” package will allow us to create a tarball from PM2 and all its dependencies once we have downloaded it.
When the package has been globally installed we need to download PM2 in a local folder. Do this be executing these commands:
mkdir pm2
cd pm2
npm install pm2
After installation of PM2 on our created folder, we will pack everything:
npm-bundle pm2
The created tarball can be found in your PM2 folder.
Installation on target system (axcf2152)
Before login in on the target, we must copy the 2 created tarballs.
You can copy these easily using scp as shown below. There is a possibility that you must change the locations of the 2 files in the command, depending on in which locations you executed the npm-bundle and curl commands.
scp pm2-3.5.1.tgz admin@192.168.18.10:/opt/plcnext
scp node-v10.16.3-linux-armv7l.tar.gz admin@192.168.18.10:/opt/plcnext
Once copied, login to the target using ssh
ssh admin@192.168.18.10
Change to root user (if not yet present on controller, create by executing “sudo passwd root”) and unpack the 2 files to the /opt folder
su
tar xpf ./node-v10.16.3-linux-armv7l.tar.gz -C /opt
tar xpf ./pm2-3.5.1.tgz -c /opt
the PM2 package was extracted in a folder named package. In order to change its name go to the /opt folder and use the mv command:
cd /opt
mv package pm2
now that everything is installed, you can remove the 2 tarballs:
rm /opt/plcnext/node-v10.16.3-linux-armv7l.tar.gz
rm /opt/plcnext/pm2-3.5.1.tgz
The last step is adding the new bin locations so that the PM2, npm and node commands are known system wide:
cat <<EOF > "/etc/profile.d/node.sh"
#!/bin/sh
export PATH="/opt/node-v10.16.3-linux-armv7l/bin:/opt/pm2/bin:\$PATH"
EOF
After executing the command below:
. /etc/profile.d/node.sh
or after a relogin to the target, you should be able to use the npm, node and pm2 command. You can test them be getting their version:
node –v
result: v10.16.3
npm –v
result: 6.9.0
pm2 –v
result: [PM2] Spawning PM2 daemon with pm2_home=/home/root/.pm2
[PM2] PM2 Successfully daemonized
3.5.1
Installing a Node project
To show you how you can setup a Node.js project environment on the controller without internet connection we are going to make an example project containing the node-red package with some other modules.
On the host computer
Make a directory in which you will install all needed modules:
mkdir nodeProj
cd nodeProj
Now install the needed modules locally. As example I will download node-red and the node-red-contrib-iiot-opcua module:
npm install node-red
npm install node-red-contrib-iiot-opcua
Transfer your folder to the /opt/plcnext folder on the target:
scp –r nodeProj admin@192.168.18.10:/opt/plcnext
From as soon the transfer is done, log back in to the target:
ssh admin@192.168.18.10
Start Node-RED by executing the red.js file via PM2:
pm2 start /opt/plcnext/nodeProj/node_modules/node-red/red.js
If you want to start your project at startup then execute these commands as well:
pm2 save
pm2 startup
If you now go to the ip-address of your PLC followed by the port 1880, you should see the Node-RED environment with the opcua library in it!
Have fun tinkering!
Leave a Reply
You must be logged in to post a comment.