Last modified: Jun 23, 2026
Fix Cannot Find Module Express-Session
Encountering the error "Cannot find module 'express-session'" is common when building web apps with Express. This error stops your server from starting. It usually means the package is missing or not installed correctly. Do not worry. This guide will help you fix it step by step.
We will cover the main causes. Then we will show you clear solutions. You will learn how to prevent this error in future projects. Let's dive in.
Why This Error Happens
The error occurs because Node.js cannot locate the express-session module in your project's node_modules folder. This can happen for several reasons.
- You forgot to install the package.
- The package was installed globally instead of locally.
- You are running the script from the wrong directory.
- The
package.jsonfile is missing or corrupted. - There is a version conflict with Node.js or Express.
Understanding the root cause helps you apply the right fix. Most of the time, a simple install command solves it.
Step 1: Install express-session Locally
The most common fix is to install express-session in your project directory. Open your terminal. Navigate to your project folder. Run this command.
npm install express-session
This command downloads the package and adds it to node_modules. It also updates your package.json file. After installation, try starting your server again.
node app.js
If the error disappears, you are done. If not, proceed to the next step.
Step 2: Check Your Project Structure
Sometimes you run the script from the wrong folder. Make sure your terminal is inside the project root. The root contains the package.json file. Use the ls or dir command to verify.
ls
# You should see package.json, node_modules, and your script files
If you are in the correct folder but the error persists, check if node_modules exists. If it is missing, run npm install to restore all dependencies.
Step 3: Verify package.json
Open your package.json file. Look under the dependencies section. You should see "express-session": "^1.17.3" or a similar version. If it is missing, add it manually or reinstall.
{
"name": "my-app",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.2",
"express-session": "^1.17.3"
}
}
If the entry is there but the error still appears, delete the node_modules folder and the package-lock.json file. Then run npm install again. This cleans any corrupted files.
Step 4: Use the Correct Import Statement
Make sure you are importing the module correctly in your code. For Express apps, use require or import syntax. Here is a working example.
// app.js
const express = require('express');
const session = require('express-session'); // Correct import
const app = express();
app.use(session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: true
}));
app.get('/', (req, res) => {
res.send('Session is working!');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Notice the require('express-session') line. It must match the package name exactly. If you are using ES modules, use import session from 'express-session';.
Step 5: Check Node.js and Express Versions
Outdated versions can cause compatibility issues. Ensure you have a recent version of Node.js. Run these commands to check.
node --version
# Should be v14 or higher
npm --version
# Should be v6 or higher
Also update Express if needed. Run npm update express. Then test your app again.
Example: Full Working Code
Here is a complete example to test your setup. Create a new file called test-session.js.
// test-session.js
const express = require('express');
const session = require('express-session'); // Must be installed
const app = express();
// Configure session middleware
app.use(session({
secret: 'mySecretKey',
resave: false,
saveUninitialized: true,
cookie: { secure: false } // Set to true if using HTTPS
}));
// Route to set a session variable
app.get('/set', (req, res) => {
req.session.username = 'JohnDoe';
res.send('Session set!');
});
// Route to get session variable
app.get('/get', (req, res) => {
const username = req.session.username;
res.send(`Session username: ${username}`);
});
app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});
Run the script with node test-session.js. Then visit http://localhost:3000/set and http://localhost:3000/get in your browser. If you see the session data, everything works.
Common Pitfalls
Beginners often make these mistakes. Avoid them to save time.
- Installing globally: Use
npm install express-sessionwithout the-gflag. - Typo in package name: The name is
express-session, notexpress-sessionsorsession. - Missing
node_modules: Always runnpm installafter cloning a project. - Using wrong require path: Do not add a relative path like
./node_modules/express-session.
If you encounter other module errors, you might find this article helpful: Fix Node.js Error: Cannot find module. It covers similar issues for other packages.
Conclusion
The "Cannot find module 'express-session'" error is easy to fix. Start by installing the package locally with npm install express-session. Then check your project structure and import statements. If the problem persists, clean your dependencies and reinstall.
Always run your server from the project root. Keep Node.js and Express updated. With these steps, you will have session management working in no time.
Remember, this error is common for many Node.js packages. For a broader guide on module errors, see Fix Node.js Error: Cannot find module. Happy coding!