Updated News Around the World

How to deploy a Node.js development environment on Ubuntu Linux 22.04

Jack Wallen walks you through the steps to deploy a Node.js development environment on Ubuntu 22.04 with Node.js version 14.

Business man using computer hand close up futuristic cyber space GameFi and decentralized finance coding background, business data analytics programming network metaverse digital world technology
Image: DIgilife/Adobe Stock

Node.js is a very popular, open source back-end JavaScript runtime environment that is used for server-side development. Node.js makes it possible to execute JavaScript code outside of a web browser so you can design and build scalable applications.

I’m going to demonstrate how to deploy a Node.js development environment on the latest release of Ubuntu Server (v 22.04). Ubuntu does include a version of Node.js in the default repositories and although it is way out of date, it can still be used and is supported until 2025. However, let’s get the latest version installed — as of this writing, that would be 18.8.0.

SEE: Hiring kit: Python developer (TechRepublic Premium)

Once we have Node.js installed, we’ll deploy the Hello World application.

What you’ll need to deploy Node.js

To make this work, you’ll need a running instance of Ubuntu Linux, version 22.04 (Desktop or Server) and a user with sudo privileges. That’s it.

How to install Node.js version 18

Log in to your instance of Ubuntu, open a terminal window and add the necessary repository with the command:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Once that’s taken care of, install Node.js with:

sudo apt-get install nodejs -y

In my instance, the installation errored out and even sudo apt-get install -f couldn’t fix the problem. To solve the issue, I had to resort to dpkg and force the installation with the command:

sudo dpkg --force-all -i /var/cache/apt/archives/nodejs_18.8.0-deb-1nodesource1_amd64.deb

You will want to check the /var/cache/apt/archives/ directory to see which .deb file for Node.js was downloaded and edit the installation command to suit.

Once Node.js is installed, add a few additional packages with:

sudo apt-get install gcc g++ make

Next, you’ll want to install yarn with the following commands:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Verify the Node.js installation with the command:

node -v

You should see something like this printed out:

v18.8.0

How to create your first application

Let’s create a Hello World application. Create a new directory with the command:

mkdir ~/nodetest

Change into that directory:

cd ~/nodetest

Create the .js file with the command:

nano helloworld.js

In that file, we’ll paste the code for our Hello World application that looks like this:

//Load the required HTTP module
const http = require("http");
const hostname="127.0.0.1";
const port = 3000;

//Create an HTTP server that listens on port 3000
const server = http.createServer((req, res) => {

//Set the response HTTP header with HTTP status and Content type
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello TechRepublic\n');
});

//listen for requests on port 3000, and as a callback function have the port listened on logged
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

You can change the const hostname to the IP address of your hosting machine if need be. Save and close the file.

How to view the Hello World app

Open a web browser and point it to http://SERVER:3000, where SERVER is the IP address of the hosting server. You should see, printed in the web browser, “Hello TechRepublic.”

You can then close the web browser tab and then shut down the app with the Ctrl+C keyboard shortcut. And that’s all there is to installing the latest version of Node.js and running the Hello World application on Ubuntu Linux 22.04.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

For all the latest Technology News Click Here 

 For the latest news and updates, follow us on Google News

Read original article here

Denial of responsibility! NewsUpdate is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – [email protected]. The content will be deleted within 24 hours.