Introduction
Express is a free and open-source back end web application framework for Node.js, which allows the creation of web applications and APIs in a much easier and cleaner way.
In this tutorial, we will see how to create a package.json
file and install Express
.
Prerequisites
I am assuming you already have Node.js
and npm
installed on your system. To install Node.js
and npm
follow How to install Node.js on Ubuntu 20.04 or How To Install latest version of Node.js on Ubuntu 20.04
Create package.json file
Let’s start with creating a directory for our application.
mkdir express-app
cd express-app
Now create package.json
for our application, to do this we will use the command npm init
.
npm init
This command will prompt us with many things like the package name, version etc. You can simply press Enter to accept the default for most of them.
package name : (express-app)
If you plan to publish your package, the most important thing in your package.json
is the name. If you want to change the package name then provide here then press enter or simply press Enter to accept the default.
version: (1.0.0)
version is the second most important thing in your package.json
file after the name. to accept the default version number press Enter.
description:
You can put a description in it. Press Enter to leave it empty.
entry point: (index.js)
Here you can provide the name for the main file. For this tutorial, I want to call it app.js
entry point: (index.js) app.js
If you want it to be index.js
just press Enter.
test command:
Here you can provide the command which runs whenever you call npm test
, for now just press Enter to skip, we will discuss it in another tutorial.
git repository:
If you have set up a git repository for the application then you can provide the link here otherwise press Enter to skip.
keywords:
Here you can provide keywords that are relevant to the package that you would expect people to use while searching for a package like yours. To skip press Enter.
author:
If you want to provide author information give it here or press Enter to skip.
license: (ISC)
Any license information can be put here, press Enter to skip.
About to write to .../express-app/package.json:
{
"name": "express-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
Type ‘Y’ to confirm and press enter.
Install Express
Now our package.json
file is ready. Let’s install Express
in the express-app directory and save it in the dependencies list.
npm install express --save
Now Express has been installed and a new directory node_modules
has been created in our application directory and our package.json
file also got updated.
{
"name": "express-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Conclusion
In this tutorial, You have created a package.json
file using npm init
and successfully installed Express
version 4.17.1
. You can continue learning by following “Hello World!” Program With Node.js and Express.