ERR_UNSUPPORTED_DIR_IMPORT
Node.jsERRORNotableModuleHIGH confidence

Directory import is not supported in ES modules

Production Risk

Causes hard crashes at startup; ensure all import paths are fully specified before deploying.

What this means

Thrown when an ES module attempts to import a directory path. Unlike CommonJS, which automatically resolves index.js inside a directory, the ESM loader requires an explicit file path or a package.json "exports" field. Importing a directory directly is not supported.

Why it happens
  1. 1Writing import "./utils/" or import "./components" without specifying a file
  2. 2Migrating CJS code that relied on implicit index.js resolution to ESM
  3. 3A package missing an "exports" field in its package.json
How to reproduce

Triggered when the ESM specifier resolves to a filesystem directory rather than a file.

trigger — this will error
trigger — this will error
// index.mjs
import { helper } from './utils'; // utils/ is a directory
console.log(helper());

expected output

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/project/utils' is not supported resolving ES modules

Fix 1

Specify the exact file path

WHEN Always — be explicit about which file to import

Specify the exact file path
import { helper } from './utils/index.js';

Why this works

ESM requires fully specified paths; providing the exact filename satisfies the loader.

Fix 2

Add an "exports" field to the package.json inside the directory

WHEN When the directory is treated as a sub-package

Add an "exports" field to the package.json inside the directory
// utils/package.json
{
  "exports": {
    ".": "./index.js"
  }
}

Why this works

The "exports" field tells the ESM loader which file to resolve when the directory is imported as a package.

Code examples
Triggerjs
// index.mjs
import { helper } from './utils'; // utils/ is a directory
console.log(helper());  // this triggers ERR_UNSUPPORTED_DIR_IMPORT
Handle in try/catchjs
try {
  // operation that may throw ERR_UNSUPPORTED_DIR_IMPORT
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
    console.error('ERR_UNSUPPORTED_DIR_IMPORT:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_unsupported_dir_import(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Rely on bare directory imports

They work in CJS but are explicitly unsupported in ESM.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors