How do you fix "Error: Cannot find module" in Node.js?

← Node.js answers

Node could not resolve the path or package you required. Run npm install, check the spelling and relative path, and confirm the file exists with the right extension.

  • Package import (require("express")): run npm install, and check that the package is listed in dependencies and that node_modules is not stale. Deleting node_modules and the lock file, then reinstalling, fixes corrupted installs.
  • Relative import (require("./utils")): the path must be relative to the importing file, and the case must match on case-sensitive systems like Linux.
  • ES modules: relative imports need the full extension (import "./utils.js"), and a package.json with "type": "module" changes how files are loaded.
  • Wrong working directory: scripts started from another folder resolve packages from the file's location, not from where you ran the command, so check the entry point's path.
  • Build output: if you run compiled code, make sure the build step produced the file and that you start the compiled entry, not the source.

The error message prints a Require stack listing each file in the import chain, so start from the first entry to see which file asked for the missing module.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required