Skip to content

ExpressJS - Installation

Welcome to your Express.js journey! Before you can start building powerful web applications, you need a solid development environment. This guide will walk you through setting up a modern, professional Node.js project.

The only prerequisites are Node.js and its package manager, npm, which comes bundled with Node.js. To verify they are installed, open your terminal and run:

# Check Node.js version
node -v
# v18.18.0 or higher is recommended
# Check npm version
npm -v
# v9.8.1 or higher is recommended

Every Node.js project starts with a package.json file. This file acts as the heart of your project, containing metadata (like the project name and version) and, most importantly, a list of its dependencies.

Step 1: Create a Project Directory Create a new folder for your project and navigate into it.

mkdir hello-world
cd hello-world

Step 2: Initialize the Project Run the npm init command. It will ask you a series of questions to create your package.json file. You can press Enter to accept the defaults or use the -y flag to skip all questions.

# Interactive setup
npm init
# Or, to accept all defaults immediately
npm init -y

This creates a package.json file that looks something like this:

{
"name": "hello-world",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Step 3: Install Express Now, let’s install the Express framework. We’ll use the npm install command. The package will be downloaded into a node_modules directory, and a package-lock.json file will be created to lock the exact versions of your dependencies for consistency across environments.

# Installs express and adds it as a dependency in package.json
npm install express

After running this, your package.json will be updated with a new dependencies section:

"dependencies": {
"express": "^4.18.2"
}

During development, you’ll be constantly changing files. Manually stopping and restarting the server each time is tedious. nodemon is a tool that automatically restarts your Node.js application when it detects file changes.

Best Practice: Install development tools like nodemon as dev dependencies. They are only needed for development and not for the final production application. Use the --save-dev (or -D) flag.

npm install --save-dev nodemon
# or the shorter version
npm i -D nodemon

Using npm Scripts for a Professional Workflow

Section titled “Using npm Scripts for a Professional Workflow”

Instead of running commands like node index.js or nodemon index.js directly, the standard practice is to define them in the scripts section of your package.json. This makes your project easier for you and others to use.

Update your package.json’s scripts section as follows:

"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
}

Now you can use these simple commands:

  • npm start: To run your application in production mode.
  • npm run dev: To run your application in development mode with nodemon for auto-restarts.

Before you write any code, it’s a great idea to initialize a Git repository to track your changes.

# Initialize a Git repository
git init
# Create a .gitignore file to exclude node_modules and other clutter
# Add 'node_modules/' and other files (like .env) to your .gitignore

Your development environment is now fully configured and ready for building amazing things with Express!