Last modified: Jun 23, 2026

Fix Helmet Module Error in Node.js

If you are working on a Node.js project and see the error Cannot find module 'helmet', do not worry. This is a common issue for beginners and experienced developers alike. It usually happens when the helmet package is not installed or not properly linked in your project. In this guide, we will walk through the causes and solutions step by step.

What is Helmet?

Helmet is a popular security middleware for Express.js applications. It helps protect your app from well-known web vulnerabilities by setting various HTTP headers. When you try to import or require it in your code, Node.js looks for it in the node_modules folder. If it is missing, you get the error.

Common Causes of the Error

There are a few reasons why this error appears. The most common ones are:

  • You forgot to install Helmet in your project.
  • You installed Helmet globally instead of locally in your project.
  • Your node_modules folder is corrupted or incomplete.
  • You are running the script from the wrong directory.
  • There is a typo in the module name.

How to Fix the Error

Follow these steps to resolve the issue quickly. Each step includes code examples and explanations.

Step 1: Install Helmet Locally

The most straightforward fix is to install Helmet in your project directory. Open your terminal, navigate to your project root, and run:


npm install helmet

This command adds Helmet to your node_modules folder and updates your package.json file. After installation, try running your application again.

Step 2: Check Your package.json

Sometimes the package is listed in package.json but not installed. Verify that Helmet appears under dependencies:


{
  "dependencies": {
    "helmet": "^7.0.0"
  }
}

If it is missing, add it manually and run npm install again.

Step 3: Delete node_modules and Reinstall

If your node_modules folder is corrupted, delete it and reinstall all dependencies. This is a common fix for many Node.js errors, including the Cannot find module 'helmet' issue. Run these commands:


# Delete node_modules folder
rm -rf node_modules

# Delete package-lock.json (optional but recommended)
rm package-lock.json

# Reinstall all dependencies
npm install

After this, your project should have a fresh copy of Helmet and all other packages.

Step 4: Ensure You Are in the Right Directory

Sometimes developers run the script from a parent folder or a different location. Make sure your terminal is in the same directory as your package.json file. You can check by running ls (Linux/Mac) or dir (Windows) to see if the file exists.

Step 5: Check for Typos in the Module Name

A simple typo can cause this error. In your code, ensure you are using the correct spelling: require('helmet') or import helmet from 'helmet'. Avoid extra spaces or uppercase letters.

Example Code with Helmet

Here is a complete example of a basic Express server using Helmet. This will help you test if the installation worked.


// Import express and helmet
const express = require('express');
const helmet = require('helmet'); // Ensure this line does not throw error

const app = express();

// Use helmet middleware
app.use(helmet());

// Define a simple route
app.get('/', (req, res) => {
  res.send('Hello World with Helmet security!');
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});

If you run this code after installing Helmet, you should see the server start without errors. The output in your terminal will be:


Server running on http://localhost:3000

Additional Troubleshooting Tips

If the error persists, try these advanced steps:

  • Clear npm cache: Run npm cache clean --force and then reinstall Helmet.
  • Use a specific version: Sometimes the latest version has issues. Install a stable version like npm install helmet@6.0.0.
  • Check for global installation: If you installed Helmet globally (npm install -g helmet), it will not be available in your project. Always install locally.
  • Update Node.js and npm: Older versions may have compatibility issues. Update to the latest LTS version.

Related Article

For more general guidance on fixing module errors, check out our article on how to Fix Node.js Error: Cannot find module. It covers similar issues with other packages.

Preventing Future Errors

To avoid this error in the future, always install packages using npm install with the --save flag (default in npm 5+). Also, use a .gitignore file to exclude node_modules from version control, but always run npm install after cloning a project.

Conclusion

The error Cannot find module 'helmet' is easy to fix. Most of the time, running npm install helmet in your project directory solves it. If not, check your node_modules folder, delete and reinstall dependencies, or ensure you are in the correct directory. By following the steps in this guide, you can get your Node.js application running with Helmet security in no time. Remember to always test your code after making changes to confirm the error is resolved.