🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

load-esm

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

load-esm

Utility to dynamically load ESM modules in TypeScript CommonJS projects

0.1.0
Source
npm
Version published
Weekly downloads
1.4M
7.67%
Maintainers
0
Weekly downloads
 
Created
Source

load-esm

load-esm is a utility for dynamically importing pure ESM (ECMAScript Module) packages in CommonJS TypeScript projects. This is particularly useful when working with Node.js environments configured for CommonJS while needing to integrate ESM-only dependencies.

Installation

npm install load-esm

or

yarn add load-esm

Features

  • Enables dynamic import of pure ESM modules in CommonJS-based projects.
  • Compatible with TypeScript, offering proper type definitions.
  • Simple and lightweight implementation.

Usage

Here’s an example demonstrating how to use load-esm to dynamically load an ESM module in a CommonJS project:

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);
    }
})();

API

loadEsm<T = any>(name: string): Promise<T>

Dynamically imports an ESM module.

Parameters

  • name (string): The name or path of the module to load.

Returns

  • A Promise<T> that resolves to the imported module object.

Example

import { loadEsm } from 'load-esm';

(async () => {
  const module = await loadEsm('some-esm-module');
  console.log(module);
})();

How It Works

The utility leverages Node.js's import() function to dynamically load ESM modules. This allows CommonJS-based projects to interact with ESM dependencies without converting the entire project to ESM.

Compatibility

  • Node.js: Requires Node.js 13.2.0 or later, as import() is only supported in these versions and beyond.
  • TypeScript: Fully typed and compatible with TypeScript.

Why Use load-esm?

In mixed-module environments where your project is primarily CommonJS but relies on some ESM-only dependencies, load-esm serves as a seamless bridge, enabling interoperability without changing your project’s module system.

License

MIT

Keywords

load-esm

FAQs

Package last updated on 09 Dec 2024

Did you know?

Socket

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.

Install

Related posts