Last modified: Jun 23, 2026

Fix Cannot find module 'compression'

Encountering the error "Cannot find module 'compression'" in Node.js can stop your app from running. This common issue happens when the compression middleware is missing or not installed properly. Don't worry, fixing it is straightforward. This guide will walk you through the causes and solutions step by step.

We will cover what the compression module does, why the error appears, and how to resolve it. You will also learn how to prevent similar errors in the future. Let's get started.

What is the compression module?

The compression module is a popular Node.js middleware for Express apps. It compresses HTTP responses using gzip or deflate. This reduces the size of data sent to the client, speeding up your web application. It is often used in production to improve performance.

When you try to require this module without installing it, Node.js throws the "Cannot find module" error. The module is not part of the Node.js core library. You must install it separately using npm or yarn.

Why does this error occur?

This error occurs for a few simple reasons. The most common is that you forgot to run npm install compression. Another reason is that the module is missing from your node_modules folder. This can happen after cloning a project without running the install command.

Sometimes, the error appears due to a version mismatch. For example, if your package.json lists an older version of compression, npm might fail to install it. A typo in the require statement can also cause this issue. Check your code carefully.

How to fix the error

Follow these steps to resolve the error quickly. Start with the simplest solution and move to advanced ones if needed.

Step 1: Install the compression module

The most direct fix is to install the module. Open your terminal in the project root directory and run:

npm install compression

If you use yarn, run:

yarn add compression

This command downloads the module and adds it to your node_modules folder. It also updates package.json and package-lock.json. After installation, try running your app again.

Step 2: Check your require statement

Ensure you are using the correct require statement. The module name must be spelled exactly as 'compression'. Here is an example of the correct usage in an Express app:

// Import the compression module
const compression = require('compression');
const express = require('express');

const app = express();

// Use compression middleware for all routes
app.use(compression());

app.get('/', (req, res) => {
  res.send('Hello, world!');
});

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

If you see the error again, double-check the spelling. A common mistake is writing 'comprssion' or 'compresson'. Also, make sure the require statement is at the top of your file.

Step 3: Rebuild node_modules

If installation does not work, delete the node_modules folder and package-lock.json file. Then reinstall all dependencies. This clears any corrupted files. Run these commands:

rm -rf node_modules package-lock.json
npm install

This forces npm to fetch fresh copies of all modules, including compression. After this, the error should be gone.

Step 4: Check your Node.js version

The compression module works with Node.js versions 0.10 and above. If you use an older version, upgrade Node.js. You can check your version with:

node --version

If it is below 0.10, download the latest LTS version from the official Node.js website. This also ensures better performance and security.

Example with error output

Here is what the error looks like when it appears. Run this code without installing compression first:

// Example that causes the error
const compression = require('compression'); // Module not installed
console.log('App started');

Output:

Error: Cannot find module 'compression'
Require stack:
- /path/to/your/app.js
  at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
  at Function.Module._load (node:internal/modules/cjs/loader:778:27)
  at Module.require (node:internal/modules/cjs/loader:1005:19)
  at require (node:internal/modules/cjs/helpers:102:18)
  at Object. (/path/to/your/app.js:1:23)
  ...

After installing the module, the same code runs without errors. The output is simply "App started". This shows how important it is to install dependencies first.

Preventing the error in the future

To avoid this error, always run npm install after cloning a project. This installs all dependencies listed in package.json. Also, use a .gitignore file to exclude node_modules from version control. This keeps your repository clean.

Another tip is to check your package.json for the compression entry. If it is missing, add it manually or use the --save flag during installation. For example:

npm install compression --save

This ensures the module is listed as a dependency. If you encounter other module errors, read our guide on Fix Node.js Error: Cannot find module for more tips.

Common mistakes to avoid

Beginners often make these mistakes. First, they install the module globally instead of locally. Global installations do not work with require statements in your project. Always install locally using npm install compression without the -g flag.

Second, they forget to restart the server after installation. Node.js caches modules, but a restart ensures the new module is loaded. Stop your server with Ctrl + C and start it again.

Third, they use a different casing in the require statement. Node.js module names are case-sensitive on some operating systems. Always use lowercase for 'compression'.

Conclusion

The "Cannot find module 'compression'" error is easy to fix. Install the module with npm install compression, check your require statement, and rebuild node_modules if needed. Always run npm install after cloning a project to avoid similar issues.

By following these steps, your Express app will run smoothly with compression enabled. This improves performance and user experience. If you need more help, refer to the Fix Node.js Error: Cannot find module guide for other common errors.