Node.js is on the rise in the IBM i ecosystem. People are taking notice, but there is still a misconception among some that Node.js and other open source software are difficult to install on IBM i. This article aims to show just how easy peasy it is to get Node.js installed, configured, and running fine on IBM i.
Prerequisite
This article assumes that you have yum installed and accessible on your IBM i instance. Please visit IBM i’s OpenSource Repository on BitBucket for installation instructions. Installing yum is quite simple. It’s just a few button clicks in Access Client Solutions.
Note: My BASH dotfiles will help create a friendlier command line interface when you SSH to your IBM i. Please take a look at my IBM i Dotfiles blog post to find out more.
Node.js Installation
yum install nodejs
When you run this command, you will be prompted with a Y/n question to confirm installation. Yes is default, so just press enter
, or press y
and enter
if you want to be explicit.
It’s that easy. Node.js is now installed and ready to run on your IBM i. Now what do you do?
Hello World (Command Line)
Let’s make a really simple “Hello World” application with Node.js that will print Hello World to the console. A majority of people use Node.js to serve web applications, but keep in mind that Node.js at its core is a scripting language, and so it can be used to write simple command line scripts as well. That’s right System Administrators: you can write powerful command line scripts with Node.js, especially when combined with Db2 and IBM i Toolkit.
Note: For simplicity, I will be using command line interfaces throughout this tutorial. For example, using mkdir to create directories. Feel free to use your favorite GUI to accomplish the same tasks. For example, you could use Windows File Explorer to create the directory and VSCode to edit the file.
1. Create Node.js Project Directory
Run these commands to create the project directory and change directory to the project directory.
mkdir /home/USERNAME/Projects/hello-world
cd /home/USERNAME/Projects/hello-world
2. Create Script
Create a script, let’s name it app.js, and add this to the file:
// app.js - Hello World example
console.log("Hello World!");
3. Run Script
Now we will run the script via command line.
node app.js
If you’re familiar with PHP, Python, and other scripting languages, this should look very familiar to you. It’s the same as php script.php
and python script.py
.
After running this command, you should see Hello World!
printed in your console.
Hello World (Web Application)
Node.js is wonderful in that it has its own built in web server. No Apache, Nginx, etc. necessary. You define the web server in the application itself, run the application, and that’s all you need to do. There are plenty of options for serving your Node.js application in production, such as PM2, but we’ll save advanced Node.js web application topics for another time. For now, let’s get a simple Hello World web application running.
1. Update app.js
In the same app.js file from earlier, replace the file content with:
// app.js - Hello World Web Application
const http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World!\n');
}).listen(3000);
console.log('Server started');
With just a few lines, we have defined a Node.js web application that will print Hello World!
to the user.
2. Run app.js
Just as we did before, run the script from command line.
node app.js
This time, you should see Server started
printed to your console.
Note: To stop the application, simply press ctrl
and c
together. This will stop any console program that you’re running at the time.
3. View Web Application
Visit the address we defined for our web application in a web browser by going to the URL http://<ibm-i-ip>:3000
. You should plainly see Hello World!
printed to the browser.
Summary
It’s time to peel back the misconceptions surrounding IBM i Open Source. With the delivery of Yum to the platform, we now have access to hundreds of open source software, all curated by the IBM i team. With a few keystrokes and approximately six lines of code, we have a Node.js web application up and running on IBM i.
Now that you know it’s so easy, go forth and produce beautiful Node.js applications. If you have any questions about Node.js, including help with mentoring, please reach out.
Peter Price
Josh
Carl
Josh
JB
Josh