
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Supply Chain Security
Vulnerability
Quality
Maintenance
License
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
load-esm is a utility for dynamically importing pure ESM (ECMAScript Module) packages in CommonJS TypeScript projects.
This resolves the error:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in...
npm install load-esm
or
yarn add load-esm
Here’s a conceptual example demonstrating how to use load-esm to dynamically load an ESM module in a CommonJS project:
import {loadEsm} from 'load-esm';
/**
* Import 'file-type' ES-Module in CommonJS Node.js module
*/
(async () => {
const esmModule = await loadEsm('esm-module');
})();
A concrete example loading file-typ, a pure ESM package:
import * as path from 'path';
import {loadEsm} from 'load-esm';
/**
* Import 'file-type' ES-Module in CommonJS Node.js module
*/
(async () => {
try {
// Dynamically import the ESM module
const { fileTypeFromFile } = await loadEsm('file-type');
// Use the imported function
const type = await fileTypeFromFile('fixture.gif');
console.log(type);
} catch (error) {
console.error('Error importing module:', error);
}
})();
loadEsm<T = any>(name: string): Promise<T>
Dynamically imports an ESM module.
name
(string): The name or path of the module to load.Promise<T>
that resolves to the imported module object.import { loadEsm } from 'load-esm';
(async () => {
const module = await loadEsm('some-esm-module');
console.log(module);
})();
Using await import
in a CommonJS TypeScript project poses challenges because the TypeScript compiler transpiles import()
statements to require()
calls when module is set to CommonJS in tsconfig.json
.
This behavior conflicts with the dynamic nature of import()
used for ESM.
Workarounds, such as wrapping the import()
statement within eval()
or similar constructs, can prevent TypeScript from transpiling it, but these approaches are clunky and error-prone.
The utility of load-esm bypasses the TypeScript compiler by executing the import()
outside the compilation scope.
By doing so, it maintains the intended behavior of import()
for loading ESM modules dynamically,
providing a clean and effective solution for such scenarios.
FAQs
Utility to dynamically load ESM modules in TypeScript CommonJS projects
The npm package load-esm receives a total of 1,484,227 weekly downloads. As such, load-esm popularity was classified as popular.
We found that load-esm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.