Installing Express
Create a project with npm and add Express as a dependency.
Syntax
npm install expressExpress is installed from npm, the Node.js package manager. You need Node.js installed first.
Step by step
- Create a folder and move into it.
- Run
npm init -yto create apackage.json. - Run
npm install expressto add Express.
This creates a node_modules folder and records express in your package.json dependencies. You are ready to write your first server.
Example
mkdir my-app
cd my-app
npm init -y
npm install expressWhen to use it
- A developer initialises a new Node.js project with npm init and adds Express so the team can start building API routes immediately.
- A CI/CD pipeline runs npm install to restore Express and its transitive dependencies in a clean Docker build layer.
- A monorepo workspace adds Express as a dependency only in the packages/api subfolder to keep the web package lightweight.
More examples
Init project and install Express
Creates a new Node project with default settings and adds Express as a production dependency.
mkdir my-app && cd my-app
npm init -y
npm install expressVerify Express in package.json
Shows how Express appears in package.json after installation, using a semver caret range.
{
"name": "my-app",
"version": "1.0.0",
"dependencies": {
"express": "^5.0.0"
}
}Import and confirm installation
Requires Express and logs the installed version to confirm the dependency is correctly resolved.
const express = require('express');
console.log('Express version:', require('express/package.json').version);
const app = express();
app.get('/ping', (req, res) => res.send('pong'));
app.listen(3000);
Discussion