Installing Node.js and Express on Ubuntu 20.04

Tecnologia infraestrutura e desenvolvimento

Installing Node.js and Express on Ubuntu 20.04

Funcionando

Last Updated: Thu, Jan 20, 2022

Node.jsProgrammingUbuntu

Introduction

Node.js is an open-source, cross-platform runtime environment for server-side JavaScript code execution. It uses Node Package Manager (npm) as its official software packages registry. It’s used to develop general-purpose applications both on the front-end and back-end. With a large community of contributors and good documentation, it’s a popular tool for many developers.

Express.js is a minimal and flexible Node.js web application framework. It’s the most popular framework for Node.js and provides a wide range of web and mobile application development features. There are several other frameworks based on Node.js. It has a large community with many libraries that solve most of the problems encountered during application development.

This article explains how to install Node.js and Express.js on Ubuntu 20.04. You will have a simple Express application running at the end of this guide.

Prerequisites

1. Install Node.js

To install Node.js, you can either install a more stable version of Node.js from the official Ubuntu 20.04 repositories, install the latest versions using Personal Package Archive (PPA) from NodeSource, or install using Node Version Manager (nvm). This tool allows you to manage multiple versions of Node.js within the same server. The stable version is dependent on the version of the operating system you are using. Conversely, the PPA versions let you choose a range of the latest versions available: the maintenance LTS version, the active LTS version, and the current release. Choose your preferred installation method and continue.

Option 1: Install Stable Version

  1. Install the Node.js stable version. $ sudo apt install nodejs -y
  2. Install npm. $ sudo apt install npm -y
  3. Verify the installed Node.js version. $ nodejs --version
  4. Verify the installed npm version. $ npm --version

Option 2: Install via PPA Version

The currently available versions of Node.js from NodeSource are v12.x, v14.x, and v16.x. To find more about the versions available, please visit the official download page. This article illustrates the installation of the latest version v16.x.

  1. Download and install the PPA to add the NodeSource repository signing keys, Node.js, and npm binaries. Then, change 16.x with your preferred version. $ curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
  2. Install Node.js 16.x, which also installs npm. $ sudo apt install nodejs
  3. Verify the installed Node.js version. $ node --version
  4. Verify the installed npm version. $ npm --version

Option 3: Install via Node Version Manager

  1. Download and install the nvm script. To find the latest version of the installation script, please visit the nvm official GitHub page. $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
  2. Verify the installed nvm version. $ nvm -v
  3. List the available versions of Node.js. $ nvm ls-remote
  4. Install the latest available version of Node.js. $ nvm install node
  5. Install version v14.3.0. $ nvm install 14.3.0
  6. List the installed versions of Node.js in the system. $ nvm ls
  7. Change to a different version. $ nvm use 14.3.0

2. Install Express.js

  1. Install Express globally in case you need it in other projects. $ npm install -g express
  2. Create your project directory. Change myproject to your preferred project name. $ mkdir myproject
  3. Navigate into the project root directory. $ cd myproject
  4. Run the npm init command to create a package.json file for your application. $ npm init -y
  5. Install Express.js in the working directory and save it in the dependencies list. $ npm install express
  6. Create an app.js file. $ sudo nano app.js
  7. Add the following Hello World code to the file. const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
  8. Save and close the file.
  9. Open firewall port 3000. $ sudo ufw allow 3000/tcp
  10. Run the application. $ node app.js

3. Access Express.js

By default, the Express.js application starts on HTTP port 3000. Open a browser and visit http://Server-IP:3000. For example:

http://192.0.2.10:3000

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *