Informing the IBM Community

Node.js v20 on IBM i

5
(3)

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

Bash
node --version

which node

This should return the Node.js version 20.8.1 as was seen in ACS.

And that is the installation out the way.

Node.js Versions

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.

Bash
## install update alternatives
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.

Bash
## run the alternatives program
alternatives --config node

If you wish to select which Python package to use, just change node to python on the alternatives command.

Bash
## run the alternatives program
alternatives --config python

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/ 

Node.js Releases

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.

JavaScript
// run index - only allow read only access to the f_mtd directory
node index.jsexperimental-permissionallow-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():

JavaScript
// A well-formed string with an emoji 
const wellFormed = "Hello ⚓ "; 

console.log(wellFormed.isWellFormed()); // true 

// An ill-formed string with a lone leading surrogate 
const illFormed = "Hello \uD83C"; 

console.log(illFormed.isWellFormed()); // false 

String.prototype.toWellFormed():

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():

JavaScript
code
// An ill-formed string with a lone leading surrogate
const illFormed = "Hello \uD83C";

console.log(illFormed); // Hello �

// Using String.prototype.toWellFormed() to convert the string
const wellFormed = illFormed.toWellFormed();

console.log(wellFormed); // Hello �
console.log(wellFormed.isWellFormed()); // true

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.

Conclusion

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.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 3

No votes so far! Be the first to rate this post.


Comments

One response to “Node.js v20 on IBM i”

  1. Great article Andy – Appreciate your thoughts on node.js 20

Leave a Reply

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