Directory import is not supported in ES modules
Production Risk
Causes hard crashes at startup; ensure all import paths are fully specified before deploying.
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.
- 1Writing import "./utils/" or import "./components" without specifying a file
- 2Migrating CJS code that relied on implicit index.js resolution to ESM
- 3A package missing an "exports" field in its package.json
Triggered when the ESM specifier resolves to a filesystem directory rather than a file.
// 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
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
// 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.
// index.mjs
import { helper } from './utils'; // utils/ is a directory
console.log(helper()); // this triggers ERR_UNSUPPORTED_DIR_IMPORTtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_unsupported_dir_import(...args) {
// validate args here
return performOperation(...args)
}✕ Rely on bare directory imports
They work in CJS but are explicitly unsupported in ESM.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev