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.
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.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.package-lock.json out of sync with node_modules can leave the package half-written.-g puts nodemailer in a global location your project's require does not search.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.jsonnpm cache clean --forcenpm 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
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.
References
FAQ
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
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.
© 2026 Courier. All rights reserved.