Nodemailer

Error: Cannot Find Module "nodemailer"

Fix the Node.js "Cannot find module 'nodemailer'" (MODULE_NOT_FOUND) error: install nodemailer, restore node_modules, run from the project root, and add types.

Updated Jul 1, 2026

The short answer

"Cannot find module 'nodemailer'" is a Node.js MODULE_NOT_FOUND error: the runtime ran your require()/import but couldn't locate the nodemailer package in node_modules. It almost always means the package was never installed, node_modules is missing or stale, or you're running from the wrong directory. Fix it by running npm install nodemailer in your project root, then re-run your script.

This is Node.js's MODULE_NOT_FOUND error (for ES modules, ERR_MODULE_NOT_FOUND). When your code runs require('nodemailer') or import nodemailer from 'nodemailer', Node walks its module-resolution algorithm — checking node_modules in the current directory and every parent directory — and throws this error when no matching package is found. It is a resolution failure, not a Nodemailer bug: the library code never even loads.

What causes "Cannot find module 'nodemailer'"?

  • nodemailer was never installed. The most common cause — you wrote the require/import but never ran npm install nodemailer, so it isn't in node_modules or package.json.
  • node_modules is missing or stale. You cloned a repo or pulled changes but never ran npm install, or node_modules was deleted/git-ignored. The dependency is listed in package.json but not on disk.
  • Running from the wrong directory. Node resolves node_modules relative to the file being executed and its parents. Running your script from outside the project (or a sibling folder) means Node never finds the project's node_modules.
  • Corrupted install or lockfile mismatch. A partial install, npm cache corruption, or a package-lock.json out of sync with node_modules can leave the package half-written.
  • Global vs. local install. Installing with -g puts nodemailer in a global location your project's require does not search.

How do I fix "Cannot find module 'nodemailer'"?

1. Install nodemailer in your project root (the folder containing package.json). If you have no package.json yet, run npm init -y first.

npm install nodemailer

This adds the dependency to package.json:

{
"dependencies": {
"nodemailer": "^9.0.0"
}
}

(The --save flag from older guides is unnecessary — npm 5+ saves to dependencies by default.)

2. If it's already in package.json but still missing, restore node_modules:

npm install

3. If the install looks corrupt, do a clean reinstall:

rm -rf node_modules package-lock.json
npm cache clean --force
npm install

4. Confirm you're running from the right place. Run your script from the project root, and make sure the file lives inside the project tree so Node's upward node_modules search reaches it.

5. TypeScript users: Nodemailer ships without bundled type declarations, so a .ts import can also surface a "Cannot find module" / "could not find a declaration file" complaint. Install the community types:

npm install --save-dev @types/nodemailer

Version note

Nodemailer's package.json declares a permissive engine requirement — "engines": { "node": ">=6.0.0" } — so the library installs on Node.js v6.0.0 and newer (its own examples that use async/await need v8.0.0+). In practice you should run a currently supported Node.js LTS release anyway, both for security and because tooling around your project may demand it. Either way, the Node version is rarely the actual cause of this error; a missing install is. If you do want to pin the library, check the latest published major on npm (the current line is Nodemailer 9.x) with npm view nodemailer version.

FAQ

Common questions

No. It's a Node.js module-resolution error (MODULE_NOT_FOUND) thrown before any email code runs, because the nodemailer package isn't installed or can't be located in node_modules. No SMTP connection is ever attempted.

One API, every provider

Stop debugging raw provider errors

Courier connects to your email, SMS, and push providers, handles retries and failover, and surfaces delivery errors in plain language.

Last reviewed Jul 1, 2026. Courier is not affiliated with third-party providers; error behavior may vary by implementation.