node · npm
The message
Error: Cannot find module 'express'
What it means
node could not resolve that name to any file: it is not in node_modules, or you ran from a directory that has no node_modules. It happens when you never installed, when an install failed halfway and left a partial tree, or when the package is in devDependencies and you installed with --omit=dev. rm -rf node_modules && npm install rebuilds the tree from the lockfile; it is safe, loses nothing, and costs only the download time. But if the name in the quotes is a relative path starting with ./, nothing is missing from your packages — the path in your own code is wrong.
The fix
rm -rf node_modules && npm install- Printed by
- node
- npm
- 20
With npm the cause sits in the first code XXXX line rather than the last six npm ERR! lines, and when the failure comes from the dependency tree or a native build instead of your own code, deleting node_modules and installing again clears about half of them.
Reading an error message
- Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
- If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
- Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
- The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
- Before pasting a fix, check what it throws away. Some of these cannot be undone.
Common questions
Q. What does “Error: Cannot find module 'express'” mean?
node could not resolve that name to any file: it is not in node_modules, or you ran from a directory that has no node_modules. It happens when you never installed, when an install failed halfway and left a partial tree, or when the package is in devDependencies and you installed with --omit=dev. rm -rf node_modules && npm install rebuilds the tree from the lockfile; it is safe, loses nothing, and costs only the download time. But if the name in the quotes is a relative path starting with ./, nothing is missing from your packages — the path in your own code is wrong.
Q. How do I fix it?
rm -rf node_modules && npm install — before running it, check the explanation above for what this command discards.
Q. Which tool prints this?
node. It sits under npm, and the message runs to 5 words.