Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js ES modules fail with ERR_UNSUPPORTED_DIR_IMPORT

ErrorError [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import

Short 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.

  • Applies to Node.js 22, Node.js 24, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 ./models
  2. 2Match what you see, then jump to its fix

    1. 1The path in the error is a directory such as ./models or ./routesA relative import points at a folder instead of a fileFix
    2. 2The project ran before type module (or an .mjs entry) was addedpackage.json was switched to type module and old require-style paths remainFix

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_modules

2Fix

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.js

Confirmed by nodejs.org

Did this fix it?

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/null

2Fix

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.js

Confirmed by nodejs.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.