🚀 DAY 3 OF LAUNCH WEEK: Introducing Webhook Events for Pull Request Scans.Learn more

load-esm

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
l

load-esm

Utility to dynamically load ESM modules in TypeScript CommonJS projects

100

Supply Chain Security

100

Vulnerability

95

Quality

85

Maintenance

100

License

Trivial Package

Supply chain risk

Packages 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

Version
1.0.0
Version published
Weekly downloads
1.9M
-15.62%
Maintainers
1
Weekly downloads
 
Created
Issues + PRs
0

NPM version

load-esm

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...

Installation

npm install load-esm

or

yarn add load-esm

Usage

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

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

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.

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.

License

MIT

FAQs

Package last updated on 13 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