
Product
A Fresh Look for the Socket Dashboard
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
load-esm is a utility for dynamically importing pure ESM (ECMAScript Module) packages in CommonJS TypeScript projects.
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 {loadModule} from 'load-esm';
/**
* Import 'file-type' ES-Module in CommonJS Node.js module
*/
(async () => {
const esmModule = await loadModule('esm-module');
})();
A concrete example loading file-typ, a pure ESM package:
import * as path from 'path';
import {loadModule} from 'load-esm';
/**
* Import 'file-type' ES-Module in CommonJS Node.js module
*/
(async () => {
try {
// Dynamically import the ESM module
const { fileTypeFromFile } = await loadModule('file-type');
// Use the imported function
const type = await fileTypeFromFile('fixture.gif');
console.log(type);
} catch (error) {
console.error('Error importing module:', error);
}
})();
loadModule<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 { loadModule } from 'load-esm';
(async () => {
const module = await loadModule('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,141,304 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 0 open source maintainers 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
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
Industry Insights
Terry O’Daniel, Head of Security at Amplitude, shares insights on building high-impact security teams, aligning with engineering, and why AI gives defenders a fighting chance.
Security News
MCP spec updated with structured tool output, stronger OAuth 2.1 security, resource indicators, and protocol cleanups for safer, more reliable AI workflows.