Node.js "ERR_UNSUPPORTED_DIR_IMPORT" During Build in CI
In ESM, importing a directory does not implicitly load its index.js the way CommonJS does. An import that points at a folder throws ERR_UNSUPPORTED_DIR_IMPORT.
What this error means
After moving to "type": "module" or running a built ESM bundle, a directory-style import fails in CI even though the same path worked under CommonJS.
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import
'/work/repo/dist/utils' is not supported resolving ES modules imported
from /work/repo/dist/index.js
code: 'ERR_UNSUPPORTED_DIR_IMPORT'Common causes
Importing a folder under ESM
ESM requires a fully specified file path. import x from "./utils" resolves a directory and is rejected; CommonJS used to silently load utils/index.js.
Build output kept extensionless directory imports
A compiler configured for CommonJS-style resolution emitted directory imports that are invalid for the ESM runtime.
How to fix it
Import the explicit index file
Reference the file directly, including its extension, so the ESM resolver has an exact target.
// before
import { fn } from './utils';
// after
import { fn } from './utils/index.js';Configure the compiler for Node ESM resolution
Set TypeScript module resolution to a Node ESM mode so it emits and checks fully specified paths.
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}How to prevent it
- Always use fully specified file paths (with extensions) in ESM source.
- Use
module/moduleResolutionofNodeNextso the compiler enforces ESM paths. - Run the built ESM bundle in CI, not just the dev/transpile path.