
Node.js is an open-source, cross-platform runtime environment designed for executing JavaScript code on the server side. By installing Node.js on Ubuntu, you gain access to its powerful capabilities, including the Node Package Manager (npm), the official registry for software packages. Widely used for developing both front-end and back-end applications, Node.js is supported by a large developer community and extensive documentation, making it an essential tool for modern 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.
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.
Install the Node.js stable version.
Install npm.
Verify the installed Node.js version.
Verify the installed npm 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.
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.
Install Node.js 16.x, which also installs npm.
Verify the installed Node.js version.
Verify the installed npm version.
Download and install the nvm script. To find the latest version of the installation script, please visit the nvm official GitHub page.
Verify the installed nvm version.
List the available versions of Node.js.
Install the latest available version of Node.js.
Install version v14.3.0.
List the installed versions of Node.js in the system.
Change to a different version.
Install Express globally in case you need it in other projects.
Create your project directory. Change myproject to your preferred project name.
Navigate into the project root directory.
Run the npm init command to create a package.json file for your application.
Install Express.js in the working directory and save it in the dependencies list.
Create an app.js file.
Add the following Hello World code to the file.
Save and close the file.
Open firewall port 3000.
$ sudo ufw allow 3000/tcp
Run the application.
By default, the Express.js application starts on HTTP port 3000. Open a browser and visit http://Server-IP:3000. For example:
You have installed Node.js and Express.js on your Ubuntu 20.04 server. Continue with the installation process by completing the required steps. You can then change the code to suit your needs.
To learn more about the tools in this article, visit:
0 Comments
Be the first to comment and share your perspective with the community.