Last modified: Jun 23, 2026

Fix Cannot Find Module 'cookie-parser'

Encountering the error "Cannot find module 'cookie-parser'" is common when working with Express.js. This error stops your server from starting. It usually means the module is missing or not installed correctly.

This guide explains why this error happens. It provides clear, step-by-step fixes. You will learn how to install the module, check your package.json, and avoid similar errors. This article is perfect for beginners and experienced developers alike.

What Does This Error Mean?

The error message looks like this:


Error: Cannot find module 'cookie-parser'
Require stack:
- /home/user/project/app.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:...)
    at Function.Module._load (node:internal/modules/cjs/loader:...)
    at Module.require (node:internal/modules/cjs/loader:...)
    at require (node:internal/modules/cjs/helpers:...)

Node.js cannot locate the cookie-parser module. This module parses HTTP request cookies. It is often used with Express to read cookies easily.

The error happens when you use require('cookie-parser') in your code but the module is not installed. It can also occur if the module is installed globally instead of locally, or if there is a version mismatch.

Common Causes

Here are the most frequent reasons for this error:

  • Module not installed: You forgot to run npm install cookie-parser.
  • Wrong directory: You are running the script from a folder without node_modules.
  • Global installation: The module is installed globally, not locally.
  • Corrupted node_modules: The folder is damaged or incomplete.
  • Missing from package.json: The dependency is not listed.

How to Fix the Error

1. Install cookie-parser Locally

Open your terminal in the project root. Run this command:


npm install cookie-parser

This installs the module into your node_modules folder. It also adds it to your package.json under dependencies.

After installation, your package.json should look similar to this:


{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "cookie-parser": "^1.4.6",
    "express": "^4.18.2"
  }
}

2. Check Your Working Directory

Make sure you are in the correct project folder. Your package.json and node_modules should be in the same directory. Use pwd (on Linux/Mac) or cd to navigate to the right place.

If you are unsure, list the files:


ls
# Output should show: package.json  node_modules  app.js

3. Delete and Reinstall node_modules

Sometimes the node_modules folder gets corrupted. Delete it and reinstall all dependencies:


rm -rf node_modules
npm install

This ensures a fresh install of all modules, including cookie-parser.

4. Verify Your Code

Check your app.js or main file. Ensure you are using the correct require statement. Here is a minimal working example:


// app.js
const express = require('express');
const cookieParser = require('cookie-parser'); // This line must work

const app = express();

// Use cookie-parser middleware
app.use(cookieParser());

app.get('/', (req, res) => {
  // Access cookies via req.cookies
  console.log(req.cookies);
  res.send('Hello, cookies!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Run the server:


node app.js
# Output: Server running on port 3000

If you still see the error, double-check the spelling. The module name is cookie-parser, not cookieParser or cookieparser.

5. Use npm init If Missing package.json

If you do not have a package.json, create one first:


npm init -y

Then install cookie-parser as shown in step 1.

Additional Tips

Always install modules locally unless you have a specific reason for global installation. Local installations keep your project self-contained and avoid conflicts.

If you are working with a team, make sure everyone runs npm install after pulling the code. This ensures all dependencies, including cookie-parser, are installed.

For a deeper dive into fixing similar module errors, check out our guide on Fix Node.js Error: Cannot find module. It covers common patterns and solutions for missing modules.

Preventing the Error in the Future

To avoid this error, follow these best practices:

  • Always run npm install <module-name> --save to add it to package.json.
  • Use version control like Git. Add node_modules to .gitignore.
  • Run npm install after cloning a repository.
  • Check your require statements for typos.

These simple habits save time and frustration.

Conclusion

The "Cannot find module 'cookie-parser'" error is easy to fix. The most common solution is to install the module locally with npm install cookie-parser. Also, verify your working directory and reinstall dependencies if needed.

By following the steps in this guide, you can get your Node.js application running smoothly. Remember to keep your package.json updated and always install modules locally. For more help with module errors, refer to our Fix Node.js Error: Cannot find module article.

Happy coding!