Node throws ERR_MODULE_NOT_FOUND for a local import in an Express ES module project
Error [ERR_MODULE_NOT_FOUND]: Cannot find moduleShort answer
In an ES module, Node does not guess file extensions or open a folder's index file. import './routes' fails, while import './routes.js' or import './routes/index.js' works. Write the full file name in every relative import, and check the path of the file you start with.
Fix it fast
1Find out which cause you have
Read the path in the error and list what is really on disk at that spot. The next lines tell you whether the problem is the extension, a folder, or the path itself.
node server.js ls -la routes2Match what you see, then jump to its fix
- 1
Cannot find module '/app/routes/users' while routes/users.js existsThe import has no file extensionFix - 2
ERR_UNSUPPORTED_DIR_IMPORT, or a folder name at the end of the pathThe import points at a folderFix - 3
The error names the program you passed to node, not an import inside itThe start file path itself is wrongFix
- 1
What does the error look like?
Error [ERR_MODULE_NOT_FOUND]: Cannot find moduleERR_UNSUPPORTED_DIR_IMPORT
Cause 1 of 3
The import has no file extension
1Check
ES module imports need the full name. The file exists, but the specifier lacks the extension.
grep -rn "from '\./\|from '\.\./" --include=*.js . | grep -v node_modules | grep -v "\.js'"2Fix
Add the extension to every relative import.
// before
import users from './routes/users';
// after
import users from './routes/users.js';3Verify
Start the server again.
node server.jsConfirmed by nodejs.org
Cause 2 of 3
The import points at a folder
1Check
Node does not resolve a folder to its index file in an import; the docs say directory indexes must be fully specified.
ls routes/index.js2Fix
Import the index file by its full path.
import router from './routes/index.js';3Verify
Start the server and request a route from that folder.
node server.js
curl -i http://localhost:3000/Confirmed by nodejs.org
Cause 3 of 3
The start file path itself is wrong
1Check
The error also fires when the entry point cannot be resolved. Check the exact path, including the folder, against what is on disk.
grep -n "\"start\"\|\"main\"" package.json
ls src2Fix
Point the start script at the file that exists, or fix the folder name.
# package.json "scripts"
"start": "node src/server.js"3Verify
Run the start script.
npm startConfirmed by nodejs.org
How do I stop it coming back?
- Write the extension in every relative import from the first line you type.
- If you compile TypeScript, write .js in the import, because that is the file Node will load.
- Run the production start command once locally, not only the dev watcher.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix