Node.js — How to Fix ESM Error “Directory import is not supported resolving ES modules imported”

ECMAScript modules are an official standard describing a format on how to write reusable JavaScript code. Browsers used ECMAScript modules and Node.js had their own module loading implementation: CommonJS. Node.js added full ESM support starting from version 12.

At the time of writing this tutorial, developers migrate their CommonJS code to use ESM. This migration surfaces a handful of compatibility errors when changing the module type. One of the errors is the directory import. This tutorial shows you how to fix directory imports when using ECMAScript modules.

Node.js Series Overview

  1. String Replace All Appearances
  2. Remove All Whitespace From a String in JavaScript
  3. Generate a Random ID or String in Node.js or JavaScript
  4. Remove Extra Spaces From a String in JavaScript or Node.js
  5. Remove Numbers From a String in JavaScript or Node.js
  6. Get the Part Before a Character in a String in JavaScript or Node.js
  7. Get the Part After a Character in a String in JavaScript or Node.js
  8. How to Check if a Value is a String in JavaScript or Node.js
  9. Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
  10. Check if a Value is a String in JavaScript and Node.js
  11. Limit and Truncate a String to a Given Length in JavaScript and Node.js
  12. Split a String into a List of Characters in JavaScript and Node.js
  13. How to Generage a UUID in Node.js
  14. Reverse a String in JavaScript or Node.js
  15. Split a String into a List of Lines in JavaScript or Node.js
  16. Split a String into a List of Words in JavaScript or Node.js
  17. Detect if a String is in camelCase Format in Javascript or Node.js
  18. Check If a String Is in Lowercase in JavaScript or Node.js
  19. Check If a String is in Uppercase in JavaScript or Node.js
  20. Get the Part After First Occurrence in a String in JavaScript or Node.js
  21. Get the Part Before First Occurrence in a String in JavaScript or Node.js
  22. Get the Part Before Last Occurrence in a String in JavaScript or Node.js
  23. Get the Part After Last Occurrence in a String in JavaScript or Node.js
  24. How to Count Words in a File
  25. How to Shuffle the Characters of a String in JavaScript or Node.js
  26. Append Characters or Words to a String in JavaScript or Node.js
  27. Check if a String is Empty in JavaScript or Node.js
  28. Ensure a String Ends with a Given Character in JavaScript or Node.js
  29. Left-Trim Characters Off a String in JavaScript or Node.js
  30. Right-Trim Characters Off a String in JavaScript or Node.js
  31. Lowercase the First Character of a String in JavaScript or Node.js
  32. Uppercase the First Character of a String in JavaScript or Node.js
  33. Prepend Characters or Words to a String in JavaScript or Node.js
  34. Check if a String is a Number
  35. Convert a String to Buffer
  36. Prevent Line Breaks in String Template Literals
  37. How to Implement a Custom `toString` Method (Coming soon)
  38. What Is `Symbol.toStringTag` and How to Use It (Coming soon)

TL;DR

ESM doesn’t support directory imports. You must import file references, with file extension:

import { something } from '../dist/index.js'   // ✅ ensure you’re importing with file extension  

Fixing the Error: Directory Import Is Not Supported

CommonJS allows directory imports by referencing a directory as a module. The module loader then looked for an index.js (or index.cjs) file within the referenced directory. This index file was used as the module and all exports from that file are available to the outside.

import { test } from 'node:test'  
import { Manager } from '../dist/index'   // 👈 this line creates the issue in ESM, but works in CJS

test('test name', () => {  
  // …
})

ESM in contrast comes with unsupported directory import. You can’t change your module type from CommonJS to ESM when using directory imports. Running your code in ESM prints the following error in the terminal:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/marcus/Projects/supercharge/framework/packages/manager/dist' is not supported resolving ES modules imported from /Users/marcus/Projects/supercharge/framework/packages/manager/test/manager.js  
    at new NodeError (node:internal/errors:406:5)
    at finalizeResolution (node:internal/modules/esm/resolve:227:11)
    at moduleResolve (node:internal/modules/esm/resolve:845:10)
    at defaultResolve (node:internal/modules/esm/resolve:1043:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36)
npm ERR! Lifecycle script `test:run` failed with error:  

In ESM, you must use file references. Change the import from the directory ../dist import to a file import '../dist/index.js' and let the ESM loader resolve the exported variables:

import { test } from 'node:test'  
import { Manager } from '../dist/index.js'   // ✅ use a file reference, with file extension

test('test name', () => {  
  // …
})

That’s it!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.