How To Install Nodejs v18 on Ubuntu 22

I installed Ubuntu 22 on my new PC. When I installed nodejs, the default version installed was v12. So I want to upgrade it to version 18.

I tried few methods but only using nvm it was successful.

Different Methods To Install NodeJS on Ubuntu 22

  1. Installing Nodejs with APT from default repositories- fail
  2. Installing Nodejs with APT using a NodeSource PPA – fail
  3. Installing Nodejs using Node Version Manager – success

Refer this tutorial from DigitalOcean

My Steps Installing Nodejs using Node Version Manager (nvm)

1) Firstly list the node version you want to install

ubuntu 22 - choose node version
ubuntu 22 – choose node version

2) Secondly, choose the version you want to install. I chose LTS v18 @ lts/hydrogen

ubuntu 22 - install node v 18
ubuntu 22 – install node v 18

3) Check if you have installed the right version.

ubuntu 22 - successfully installed node v18
ubuntu 22 – successfully installed node v18

If You Got Checksum Error

At first, I had the checksum error, when I checked on the internet it was due to curl installed using SNAP (which I did). I have to re-install my curl using APT and remove the snap path from the path environment.

ubuntu 22 - install node v 18
ubuntu 22 – install node v 18

Curl still point to snap/bin.

ubuntu 22 - curl still point to snap
ubuntu 22 – curl still point to snap

ubuntu 22 - installing node problem due to curl installed by snap
ubuntu 22 – installing node problem due to curl installed by snap

Remove the snap/bin from the environment. Edit it using nano

ubuntu 22 - edit the path at environment
ubuntu 22 – edit the path at environment

Nodejs Regex Concatenate String

I want to concatenate two strings to make a regular expression so I can have different prefix but same pattern at the back of the string.

If notice, must use doubleĀ  back slashes if the regular expression uses back slash. Example instead of \d must use \\d.

Code

Output

nodejs regex concatenate strings demo output
nodejs regex concatenate strings demo output

Demo

You can test the Regex Concatenate Strings here.

NodeJS How To Have Unique Array of Objects

I want to have unique array of objects. So if I add an object, any objects which have same values won’t be added twice.
So I try use Set() data structure.

Adding Object directly to Set()

Output

Conclusion:

As you can see, the Set result is 3. It should be 2. So it doesn’t work

Adding a stringify Object to Set()

Output:

Conclusion:

The Set size is 2 with no duplicate values.

Overall Conclusion

So you can use Set but first stringifiy the objects than convert back the string to object by using JSON.parse() function

You can try thecoding yourself here:
Typescript Playground

NodeJS Reading and Writing to WordPress PHP via Rest API

I developed NodeJS program that can read and write to WordPress Rest API. I’m having problem of reading or writing properly the data in NodeJS and WordPress and vice versa.

NodeJS Writes JSON Objects to WordPress Rest API

Any JSON String[] or Object[], will be saved into serialized PHP string by WordPress.

Surprisingly, when NodeJS read the data save in serialize string, NodeJS can read it as JSON object without needs to call JSON.parse().

However JSON Object, will be saved as string by WordPress but still follows JSON string format.

As the JSON Object is saved as string, when NodeJS read the data via WordPress Rest API, NodeJS has to call JSON.parse() to convert it back into JSON Object.

Examples of JSON Object saved by WordPress Rest API

How To Have Consistency?

By having JSON arrays save as PHP serialize string and JSON object as string in PHP, I need to have do extra checking or conversion by calling JSON.parse()

Another possible solution is to call JSON.stringify() to JSON arrays so it is saved as string at PHP. However, it is still extra step that we need to do and it doesn’t solve the problem stated above where there is no consistency.

So, in short there is no possible solution to have consistency, we still need to do extra step as mentioned above.