In this Node.js tutorial, you’ll learn how to go about setting up a Node.js project. For creating a Node.js project you need to make sure you have Node installed and running in your system.

For installing Node.js in Ubuntu you can follow the Node.js installing instructions from the official documentation.

Once you have Node.js installed, you’ll also have Node Package Manager (npm) installed on your system.

Initializing The Node Project Using NPM

Let’s start by creating a project directory for our Node.js project. Once you have the project directory, navigate to the project directory and initialize the project using npm.

mkdir node-project
cd node-project
npm init

The above command will ask for a couple details like name,version, git etc. related to the Node.js project. Enter the details and you will have the project created with a package.json file.

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

The package.json file will have all the information related to the Node.js project. Information related to all node package modules installed in the project can be saved to the package.json file using the –save option.

Creating Node.js Express Project

Let’s start by installing express framework to the Node.js project. Using npm install the express framework.

# installing express web framework
npm install express --save

The above command installs the express framework to the node-project. Now if you check the package.json file you will have the express module details saved in the package.json file as a dependency.

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4"
  }
}

Create a file called app.js and add the following code to it.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send("Welcome to setting up Node.js project tutorial!'))

app.listen(port, () => console.log(`Application listening on port ${port}!`))

Save the above changes and start the project using node app.js. You will have the application running at http://localhost:3000.