load-esm
load-esm is a lightweight utility designed for TypeScript projects configured with CommonJS (`"module": "commonjs"`) that need to dynamically load pure ECMAScript Modules (ESM) at runtime. It directly addresses common interoperability errors such as `Error [ERR_REQUIRE_ESM]: require() of ES Module` and `Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in ...`. The utility functions by executing the native `import()` call outside of TypeScript's CommonJS transpilation scope, thereby preserving the correct dynamic import semantics at runtime. This approach provides a robust and type-safe solution without resorting to brittle workarounds like `eval()`. The current stable version is 1.0.3, with releases primarily focused on documentation improvements and ensuring compatibility with evolving Node.js and TypeScript ecosystems. Its key differentiator is providing a consistent, reliable mechanism for ESM-in-CJS loading across various Node.js versions, even beyond Node.js 22.12's `require()`-based ESM loading limitations.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module path/to/esm-module.js not supported. Instead change the require of path/to/esm-module.js to a dynamic import() which is available in all CommonJS modules.
cause Attempting to `require()` a pure ECMAScript module from a CommonJS context, typically due to TypeScript transpilation changing `import()` to `require()`.fixUse `loadEsm('esm-module')` instead of `import('esm-module')` or `require('esm-module')` when in a CommonJS TypeScript project targeting pure ESM packages. -
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in package-name/package.json
cause Attempting to access a module path that is not explicitly exposed via the 'exports' field in the target package's `package.json`, common for ESM-only packages not designed for direct `require()` access.fixImport the package using `await loadEsm('package-name')` to correctly resolve its ESM entry points. -
Property 'someExport' does not exist on type 'unknown' (or 'any').
cause The dynamically loaded ESM module is not correctly typed, leading to `unknown` or `any` inference for its exports.fixProvide a type argument to `loadEsm`: `await loadEsm<typeof import('my-esm-package')>('my-esm-package')`.
Warnings
- breaking The primary function name was changed from `loadModule()` to `loadEsm()` in version 1.0.0. Projects upgrading from pre-1.0.0 versions must update their calls.
- gotcha Top-level `await` is not available in CommonJS modules. When using `loadEsm` in a CJS context, you must wrap the asynchronous operation in an async IIFE (Immediately Invoked Function Expression) or an async function.
- gotcha While Node.js versions 22.12+ offer some `require()`-based ESM loading capabilities, these come with documented constraints. `load-esm` provides a consistent and often more reliable method across various Node.js versions and complex ESM package structures.
- gotcha When loading ESM packages, ensure you provide the correct generic type argument `typeof import('package-name')` to `loadEsm` for accurate TypeScript inference. Omitting this will result in the imported module being typed as `unknown` (or `any` in older TS configurations).
Install
-
npm install load-esm -
yarn add load-esm -
pnpm add load-esm
Imports
- loadEsm
const { loadEsm } = require('load-esm');import { loadEsm } from 'load-esm'; - loadEsm (with typings)
await loadEsm<typeof import('my-esm-package')>('my-esm-package');
Quickstart
import { loadEsm } from "load-esm";
(async () => {
try {
// This example demonstrates loading the 'file-type' package, a pure ESM module.
// To run this, install 'file-type' (npm install file-type) and ensure
// 'fixture.gif' (or any valid file) exists in your project root.
// The generic type argument `typeof import("file-type")` provides full type inference.
const { fileTypeFromFile } = await loadEsm<typeof import("file-type")>(
"file-type"
);
const type = await fileTypeFromFile("fixture.gif");
console.log("Detected file type:", type); // e.g., { ext: 'gif', mime: 'image/gif' }
} catch (error) {
console.error("Error importing module or processing file:", error);
// Common errors caught here include ERR_REQUIRE_ESM and ERR_PACKAGE_PATH_NOT_EXPORTED.
}
})();