It is time to get back to Node.js on the IBM i.
Node.js v20 is the latest version of this great programming language. Please note that Node.js v14 moved out of support from April 2023, so time for you all to upgrade, and what should you upgrade to?
My recommendation is going to be Node.js v20.
Installing Node.js 20
As with all open source on the IBM i, IBM has made the process of installation very easy.
By using ACS (Access for Client Solutions), take the Available packages tab, and we can see Node.js 20 Clicking the installation tab, then finding nodejs20 will install this programming language.
To confirm everything is installed as planned, from a shell session, type
Which node –version
This should return the Node.js version 20.8.1 as was seen in ACS.
And that is the installation out the way.
As we can see in the figure below, I have two versions of Node.js on my server, versions 18 and version 20.
By using another open source package called update-alternatives, it allows us to specify which version of node we want to use. Be aware that update-alternatives will globally change what version of Node.js everyone will be using.
If you do not have update-alternatives installed on your server, run the following command in a shell session.
yum install update-alternatives
Update-alternatives can be used to manage any other open source language, for example different versions of Python.
As can be seen in the figure below, run alternatives command.
Alternatives –config node
If you wish to select which Python package to use, just change node to python on the alternatives command.
For detailed information about update-alternatives see my PowerWire article from three years ago. It can be found at this link https://powerwire.eu/bye-bye-nodever/
If you are a seasoned Node.js developer, you probably know the release schedule of Node.js like the back of your hand. But if you are juggling multiple projects and responsibilities, you might need a brief refresher on how Node.js releases work.
Each April, an even numbered release is announced. These are stable and receive long term support, also known as LTS.
Each October, odd numbered release is announced. These releases are more experimental and not ready for production use.
Features in Node.js 20
Now we have everything installed and ready to go, let me show you some of the new features of Node.js 20.
Running node somescript.js can have serious consequences. A script can perform any action, such as deleting important files, sending private data to a server, or running a hidden cryptocurrency miner. Even if your own code is safe, you cannot be sure that all the modules and their dependencies are trustworthy.
To address this issue, version 20 of Node.js introduced the new Permissions Model.
This model restricts what a script can do.
When running the node command, we can now specify what file access the node command has at run time.
If we specify the following command, it will allow read only access to the files we are specifying.
node index.js –experimental-permission –allow-fs-read=/f_mtd/
The above command will run the index.js script and only allow read only access to the /f_mtd IFS directory.
Wild cards can also be specified on the allow-fs-read argument, or parameter. To see the full list of valid parameters for this new feature, check out the official release notes, which can be found here https://nodejs.org/api/permissions.html
The next new feature was the inclusion of the latest V8 engine. This includes the following features:
String.prototype.isWellFormed() This returns true when a string is well-formed and doesn’t contain lone surrogate characters.
Unpaired surrogates can cause problems when processing strings, especially when dealing with emojis or other non-ASCII characters.
Here is a simple code example that demonstrates how to use String.prototype.isWellFormed():
String.prototype.toWellFormed() is a new feature in Node.js version 20 that converts an ill-formed string to a well-formed string by replacing unpaired surrogates with U+FFFD (REPLACEMENT CHARACTER).
This feature is useful when you want to avoid errors or unexpected behaviour when processing strings that contain non-ASCII characters, such as emojis or other Unicode symbols.
An unpaired surrogate is a code point that belongs to the range of U+D800 to U+DFFF, which are reserved for encoding UTF-16 characters in pairs. If a string contains a lone surrogate, it is not well-formed UTF-16, and it may cause problems when displaying, encoding, or decoding the string.
For example, the emoji (EARTH GLOBE AMERICAS) is encoded as U+1F30E in Unicode, but in UTF-16, it is represented by two code points: U+D83C and U+DF0E.
These two code points are called a surrogate pair, and they must always appear together in a well-formed string. If a string contains only U+D83C without U+DF0E, it is an ill-formed string with an unpaired leading surrogate.
The String.prototype.toWellFormed() method replaces any unpaired surrogates in a string with U+FFFD, which is the official Unicode character for indicating an unknown or invalid character. This way, the string becomes well-formed and can be safely processed by other methods or functions.
Here is an example of how to use String.prototype.toWellFormed():
As you can see, the String.prototype.toWellFormed() method does not change the appearance of the string, but it makes it well-formed by replacing the unpaired surrogate with U+FFFD.
You can verify that the string is well-formed by using the String.prototype.isWellFormed() method, which returns true or false.
Once again open source on the IBM i has proved it can keep up with the ever-changing landscape of open source with the inclusion of Node.js version 20.
You can find all the code examples shown in this PowerWire article, on our open-source repository on GitHub. The link to our repository is here https://github.com/PowerWire-Code/Articles
We are working hard to update all the articles examples on GitHub, but it is a huge task that will take some time. We appreciate your patience and understanding as we complete this project.
If you have any questions, either on this article, or anything else on the IBM i, please use the comments below, or contact us via X (twitter) using the handle @Powerwire
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange development experience.