How to Install ExpressJS on Ubuntu 20.04

In this article, we’ll explain how to install ExpressJS on Ubuntu 20.04.

Express.js, or simply Express Fast, unopinionated, minimalist web framework for Node.js. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

This article will guide you with the installation and creating first application by Express and Express Generator.


Install ExpressJS on Ubuntu 20.04

Keep the server updated

# apt update -y && apt upgrade -y

Install Express

Create a application directory and change current working directory.

# mkdir myapp

# cd myapp

Create a package.json file in the application directory using following command:

# npm init

This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:

entry point: (index.js)

Here you can choose any name for main file. Default file name is index.js. If you want it to be index.js, hit RETURN to accept the suggested default file name.

Finally Install Express using following command:

# npm install express --save

The installation has been completed successfully.

Next, edit index.js and add  following content:

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}`)

Save and exit.

Now, run the app using following command:

# node index.js

Note: Replace index.js with your main file name.

Then, load http://[server IP]:3000/ in a browser to see the output. This will print 'Hello World'.

Install Express Generator

Use the application generator tool, express-generator, to quickly create an application skeleton.

Install express-generator with the following command (available in Node.js 8.2.0):

# npx express-generator

For earlier Node versions, install the application generator as a global npm package and then launch it.

# npm install -g express-generator
# express

Create myapp using following command:

# express --view=pug myapp


create : myapp
create : myapp/package.json
create : myapp/app.js
create : myapp/public
create : myapp/public/javascripts
create : myapp/public/images
create : myapp/routes
create : myapp/routes/index.js
create : myapp/routes/users.js
create : myapp/public/stylesheets
create : myapp/public/stylesheets/style.css
create : myapp/views
create : myapp/views/index.pug
create : myapp/views/layout.pug
create : myapp/views/error.pug
create : myapp/bin
create : myapp/bin/www

Next, install dependencies

# cd myapp
# npm install

Now, run the app using following command:

# DEBUG=myapp:* npm start

Then load http://[server IP]:3000/ in your browser to access the app.

That’s it.

In this article, we have seen how to install ExpressJS on Ubuntu 20.04.

Related Articles