Last modified: Jun 22, 2026

Fix Cannot Find Module 'nodemailer'

You are building a Node.js app. You want to send an email. You install nodemailer. But when you run the code, you get an error: Cannot find module 'nodemailer'. This is a common problem. It stops your work. But you can fix it fast.

This article shows you why this error happens. It gives you simple steps to solve it. You will learn how to install, check, and use nodemailer correctly. We also include code examples. Let's start.

Why Does This Error Happen?

The error "Cannot find module 'nodemailer'" means Node.js cannot locate the package. Node.js looks for modules in the node_modules folder. If the folder is missing or the package is not there, you get this error.

Common causes include:

  • You forgot to install nodemailer.
  • You installed it in the wrong directory.
  • You have a typo in the package name.
  • Your package.json file is corrupt or missing.
  • You are using a different Node.js version.

Most of the time, the fix is simple. You just need to install the package again.

Step 1: Install nodemailer Correctly

Open your terminal. Navigate to your project folder. Then run this command:

npm install nodemailer

This command installs nodemailer and adds it to your node_modules folder. It also updates your package.json file. If you use Yarn, run:

yarn add nodemailer

After installation, check that the folder node_modules/nodemailer exists. If it does, the error should be gone.

Step 2: Check Your Working Directory

Make sure you are in the right folder. If you install nodemailer in one folder but run your script in another, Node.js cannot find it. Always run the install command inside your project root.

For example, if your project is at /home/user/myapp, do this:

cd /home/user/myapp
npm install nodemailer

Then run your script from the same folder:

node app.js

Step 3: Verify package.json

Open your package.json file. Look for the dependencies section. It should include "nodemailer": "^6.9.0" or a similar version. If it is missing, run npm install nodemailer --save to add it.

If your package.json is missing entirely, create one with npm init -y first. Then install nodemailer.

Step 4: Reinstall All Dependencies

Sometimes your node_modules folder gets corrupted. Delete it and reinstall everything. Run these commands:

rm -rf node_modules
npm install

This removes all packages and installs them fresh. It fixes many module errors.

Step 5: Check for Typos in Your Code

Make sure you require nodemailer correctly. The package name is all lowercase. Do not use capital letters. Here is the correct way:

const nodemailer = require('nodemailer');

If you write require('NodeMailer') or require('nodemailer-extra'), Node.js cannot find it. Always use the exact name from npm.

Step 6: Use a Transporter Example

After fixing the error, test nodemailer with a simple email. Here is a full example with comments:

// Import nodemailer
const nodemailer = require('nodemailer');

// Create a transporter object
let transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: '[email protected]',
    pass: 'your-password'
  }
});

// Define email options
let mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Test Email from Node.js',
  text: 'Hello, this is a test email!'
};

// Send the email
transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log('Error:', error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

When you run this, you should see output like:

Email sent: 250 2.0.0 OK 1234567890

If you still get the "Cannot find module" error, double-check your installation.

Step 7: Use Global Installation as Last Resort

If nothing works, try installing nodemailer globally. This is not recommended for projects, but it can help debug. Run:

npm install -g nodemailer

Then require it with the full path:

const nodemailer = require('/usr/local/lib/node_modules/nodemailer');

This is a temporary fix. For production, always install locally.

Related Error: Cannot Find Module

This error is similar to other "Cannot find module" issues in Node.js. For example, you might see it with express or fs. The steps are the same. Check your installation, directory, and package name. For more help, see our guide on Fix Node.js Error: Cannot find module.

Prevent This Error in the Future

To avoid this error, follow these tips:

  • Always run npm install after cloning a project.
  • Use a package.json file to track dependencies.
  • Do not delete the node_modules folder manually.
  • Use version control like Git to keep your project clean.

Conclusion

The "Cannot find module 'nodemailer'" error is easy to fix. First, install the package with npm install nodemailer. Then check your directory and package.json. Reinstall if needed. Always use the correct require statement. With these steps, you can send emails from Node.js without problems.

If you still have issues, review your code for typos. Use the example above to test. Remember, a clean node_modules folder and a correct package.json solve most module errors. Happy coding!