npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

load-esm

v1.0.3

Published

Utility to dynamically load ESM modules in TypeScript CommonJS projects

Readme

Node.js CI NPM version npm downloads

load-esm

load-esm is a tiny utility that lets CommonJS (CJS) TypeScript projects dynamically import pure ESM packages at runtime—without hacks like eval().

It helps avoid errors like:

  • Error [ERR_REQUIRE_ESM]: require() of ES Module
  • Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in ...

Installation

npm install load-esm
# or
yarn add load-esm
# or
pnpm add load-esm

Works in CJS TypeScript projects. No config changes required.


Quick start

// TypeScript (CJS project)
import { loadEsm } from "load-esm";

(async () => {
  const esmModule = await loadEsm("esm-module");
  // use esmModule...
})();

With typings

import { loadEsm } from "load-esm";

(async () => {
  const esmModule = await loadEsm<typeof import("esm-module")>("esm-module");
  // esmModule is fully typed
})();

Concrete example (pure ESM package)

import { loadEsm } from "load-esm";

(async () => {
  try {
    // Import a pure ESM package from a CommonJS TS project
    const { fileTypeFromFile } = await loadEsm<typeof import("file-type")>(
      "file-type"
    );

    const type = await fileTypeFromFile("fixture.gif");
    console.log(type);
  } catch (error) {
    console.error("Error importing module:", error);
  }
})();

Note: Because top‑level await isn’t available in CommonJS, examples use an async IIFE.


API

function loadEsm<T = unknown>(name: string): Promise<T>

Parameters

  • name — Package name or file path to import.

Returns

  • Promise<T> resolving to the imported module namespace.

How it works

In CJS TypeScript projects ("module": "commonjs"), the TS compiler transpiles dynamic import() to require(), which breaks when the target is a pure ESM package.

load-esm executes the import() outside of TypeScript’s transpilation scope, preserving native dynamic import() semantics at runtime. This keeps your code type‑safe while avoiding brittle workarounds (e.g., wrapping import() in eval()).

What about Node.js ≥ 22.12?

Since Node.js 22.12, require can load some ESM modules, but there are documented constraints. If your dependencies are compatible with that path, you might not need this utility. load-esm remains useful when:

  • You’re on older Node.js versions that support import() (see Compatibility) but not the newer require() behavior.
  • You want a single, consistent pattern that works across environments and avoids edge cases.

If Node’s built‑in require(esm) works for your packages and version, feel free to use it.


Compatibility

  • Node.js: ≥ 13.2.0 (first version with native import() support)
  • TypeScript: Fully typed; works in CJS projects.

Troubleshooting

  • ERR_REQUIRE_ESM: Ensure you’re using load-esm(...) to import the ESM dependency from CJS code.
  • No "exports" main defined: Some packages only expose ESM entry points. Import them via load-esm.
  • Type declarations: Use the generic form loadEsm<typeof import("pkg")>("pkg") for typed access.
  • Top‑level await: Wrap usage in an async IIFE in CJS.

License

MIT


Changelog

See Releases.


Acknowledgements

Inspired by common pain points when mixing CJS projects with modern ESM‑only libraries.