Node.js on IBM i: Easy Peasy

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.

πŸ’πŸ¦—πŸ™πŸ’»πŸŽ±πŸš² | They/Them | Coding since age 9 πŸ‘Άβž‘οΈπŸ‘¨β€πŸ’» | #Autistic w/ #CharcotMarieTooth | #IBMi + #Linux; #OpenSource #Monk; #BridgingGaps; #IBMChampion | Passionate advocate of open source and its mindset. Business owner and public speaker. Lover of animals, cooking, horror-films, hip-hop, pool, and the Oxford comma; for lists.

6 Comments

  1. Peter Price

    If you are a total novice at this who only know Green on Black the example ‘Create Script’ doesn’t help.

    How do you create a script?

    • In this case, script is synonymous with file. Use whatever method you find easiest to create a script, or file, named app.js. I left out examples, because there are dozens of ways of doing this. Since the article uses command line to describe creating a directory, I will do the same for step 2.

      After step 1, and you’re in your project directory:
      vim app.js
      [enter in file contents described and save file]

  2. Carl

    It’s not easy peasy, sorry. All of them who are thinking about writing a small piece of code – it’s not trivial. I come from the common RPG ecosystem of IBM i and a lot of work is there done. You don’t consider the callstack, no worries about promise or callback hells, sync/async and many other things. I started (and have not finished yet) with an education of javascript/node.js that let me know all these things. You have to know what dev tool (recommend VSCode from MS), which extensions, terminals and so on. Have you ever tried to install by npm packages on the IBM i. It’s not sure that things that are running on a Windows box do on the IBM i as well. There is a lot of stuff to know and to learn.

    • I’m sorry you’re having all these issues. This article is meant to convey that installing and getting started with Node.js is easy peasy. Of course, advanced topics in every language, including RPG, are inherently not going to be easy peasy.

      As for your issues with installing and compiling packages on IBM i, yes, there is definitely a learning curve. If you need any help, please feel free to ask specific questions. Otherwise, thank you for the input.

  3. JB

    hello. our server doesn’t have an internet connection but i successfully installed yum offline. how can i also install node.js without connecting to the internet? can i also do that manually?

    • Hi JB. Yes, you can install all RPMs offline by using your local internet. There’s an option for this under the Open Source Package Management UI in ACS. Just select “SSH Tunneling” under Proxy Mode while defining the connection.

      My only concern is that npm wouldn’t have an internet connection for installing node modules.

Leave Comment

Your email address will not be published. Required fields are marked *