Node.js ES modules fail with ERR_UNSUPPORTED_DIR_IMPORT
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory importShort answer
ES modules in Node.js do not resolve a folder to its index.js. An import that ends at a directory fails with ERR_UNSUPPORTED_DIR_IMPORT. Write the full path including index.js, or if you only added type module by accident, go back to CommonJS.
Fix it fast
1Find out which cause you have
Read the path in the error. It is the folder you imported. Then check whether package.json has type module, which turns on this strict resolver.
grep '"type"' package.json ls ./models2Match what you see, then jump to its fix
What does the error look like?
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import
Cause 1 of 2
A relative import points at a folder instead of a file
1Check
With import, a file extension is required for relative specifiers, and directory indexes must be fully specified, so ./startup/index.js works but ./startup does not.
grep -rn "from '\./[a-z/]*'" --include=*.js . --exclude-dir=node_modules2Fix
Add the index file to the path.
// fails
import db from './models';
// works
import db from './models/index.js';3Verify
Start the app. The error is gone.
node server.jsConfirmed by nodejs.org
Cause 2 of 2
package.json was switched to type module and old require-style paths remain
1Check
Files are ES modules when package.json has type module or the extension is .mjs. CommonJS require resolves folders, ES import does not.
grep '"type"' package.json
ls *.mjs 2>/dev/null2Fix
Either fix each import as above, or remove type module and use require again if you do not need ES modules.
// package.json: remove the line "type": "module"
const db = require('./models');3Verify
The app starts under plain node.
node server.jsConfirmed by nodejs.org
How do I stop it coming back?
- Write full relative paths with extensions in every import from the start.
- Pick one module system per project and set it in package.json.
- Search for folder imports after turning on type module, before you deploy.
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