### Install load-esm Source: https://github.com/borewit/load-esm/blob/master/README.md Install the load-esm package using npm, yarn, or pnpm. No configuration changes are required for CJS TypeScript projects. ```bash npm install load-esm # or yarn add load-esm # or pnpm add load-esm ``` -------------------------------- ### Concrete Example: Importing 'file-type' Source: https://github.com/borewit/load-esm/blob/master/README.md Demonstrates importing a pure ESM package ('file-type') from a CommonJS TS project using loadEsm. Includes error handling for the import process. ```typescript import { loadEsm } from "load-esm"; (async () => { try { // Import a pure ESM package from a CommonJS TS project const { fileTypeFromFile } = await loadEsm( "file-type" ); const type = await fileTypeFromFile("fixture.gif"); console.log(type); } catch (error) { console.error("Error importing module:", error); } })(); ``` -------------------------------- ### Basic Usage: Import Pure ESM Package with loadEsm Source: https://context7.com/borewit/load-esm/llms.txt Demonstrates the basic usage of `loadEsm` to import an npm package that is a pure ESM module. Ensure the package is installed and resolvable by Node's module resolution. ```typescript import { loadEsm } from "load-esm"; // --- Basic usage: import a pure-ESM npm package --- (async () => { const chalk = await loadEsm("chalk"); console.log(chalk.default.green("Hello from ESM!")); })(); ``` -------------------------------- ### Basic Dynamic ESM Import Source: https://github.com/borewit/load-esm/blob/master/README.md Use loadEsm to dynamically import an ESM module within a CJS TypeScript project. Examples use an async IIFE because top-level await is not available in CJS. ```typescript import { loadEsm } from "load-esm"; (async () => { const esmModule = await loadEsm("esm-module"); // use esmModule... })(); ``` -------------------------------- ### Load Local ESM File using file:// URL with loadEsm Source: https://context7.com/borewit/load-esm/llms.txt Illustrates loading a local ESM file by providing an absolute `file://` URL to `loadEsm`. The generic type parameter can be used to specify the expected type of the module's exports. ```typescript import { loadEsm } from "load-esm"; import * as path from "path"; (async () => { const modulePath = path.resolve(__dirname, "utils/helper.mjs"); const { greet } = await loadEsm<{ greet: (name: string) => string }> (`file://${modulePath}` ); console.log(greet("World")); // "Hello, World!" })(); ``` -------------------------------- ### Replacing eval() with loadEsm for ESM Imports Source: https://context7.com/borewit/load-esm/llms.txt Compares a fragile `eval()` workaround for importing ESM packages with the clean and type-safe approach using `loadEsm`. This highlights `loadEsm` as a preferred method. ```typescript // Before (fragile): // const mod = await eval('import("some-esm-pkg")'); // After (clean and type-safe): const mod = await loadEsm("some-esm-pkg"); ``` -------------------------------- ### Error Handling for Missing or Invalid Modules with loadEsm Source: https://context7.com/borewit/load-esm/llms.txt Demonstrates how to handle potential errors when using `loadEsm`, such as when a module is not found. It specifically checks for the `ERR_MODULE_NOT_FOUND` error code. ```typescript import { loadEsm } from "load-esm"; // --- Error handling for missing or invalid modules --- (async () => { try { const mod = await loadEsm("non-existent-esm-package"); console.log(mod); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ERR_MODULE_NOT_FOUND") { console.error("Module not found — check the package name or file path."); } else { console.error("Unexpected import error:", error); } } })(); ``` -------------------------------- ### loadEsm(name: string): Promise Source: https://context7.com/borewit/load-esm/llms.txt Dynamically imports a pure ESM module from a CommonJS TypeScript project. It wraps a native import() call to bypass TypeScript's CommonJS transpilation. Accepts a package name or a file:// URL. The generic type parameter T allows for full type inference from the imported module. ```APIDOC ## loadEsm(name: string): Promise ### Description Dynamically import a pure ESM module from a CommonJS TypeScript project. This function wraps a native `import()` call in a way that survives TypeScript's CommonJS transpilation. It accepts any package name resolvable by Node's module resolution or an absolute `file://` URL. The generic type parameter `T` is used to carry full type information from the imported module, giving you typed access to all its exports without runtime overhead. ### Parameters #### Path Parameters - **name** (string) - Required - The name of the package or the file path (as a `file://` URL) to import. #### Type Parameters - **T** - Used to carry full type information from the imported module. ### Returns - **Promise** - A Promise that resolves to the imported module namespace. ### Request Example ```typescript import { loadEsm } from "load-esm"; // Basic usage: import a pure-ESM npm package (async () => { const chalk = await loadEsm("chalk"); console.log(chalk.default.green("Hello from ESM!")); })(); // With full type inference (async () => { const { fileTypeFromBuffer } = await loadEsm("file-type"); const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47]); // PNG magic bytes const result = await fileTypeFromBuffer(buffer); console.log(result); // { ext: 'png', mime: 'image/png' } })(); // Loading a local ESM file by file:// URL import * as path from "path"; (async () => { const modulePath = path.resolve(__dirname, "utils/helper.mjs"); const { greet } = await loadEsm<{ greet: (name: string) => string }>( `file://${modulePath}` ); console.log(greet("World")); // "Hello, World!" })(); // Error handling for missing or invalid modules (async () => { try { const mod = await loadEsm("non-existent-esm-package"); console.log(mod); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ERR_MODULE_NOT_FOUND") { console.error("Module not found — check the package name or file path."); } else { console.error("Unexpected import error:", error); } } })(); ``` ### Response Example ```json { "example": "// The response is the module namespace of the imported ESM module." } ``` ``` -------------------------------- ### Typed Import with loadEsm and Type Inference Source: https://context7.com/borewit/load-esm/llms.txt Shows how to use `loadEsm` with a generic type parameter `` to achieve full type inference from the imported module's namespace. This provides typed access to exports without runtime overhead. ```typescript import { loadEsm } from "load-esm"; // --- With full type inference --- (async () => { const { fileTypeFromBuffer } = await loadEsm("file-type"); const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47]); // PNG magic bytes const result = await fileTypeFromBuffer(buffer); console.log(result); // { ext: 'png', mime: 'image/png' } })(); ``` -------------------------------- ### loadEsm API Signature Source: https://github.com/borewit/load-esm/blob/master/README.md The loadEsm function takes a package name or file path and returns a Promise that resolves to the imported module's namespace. It supports generic type arguments for type safety. ```typescript function loadEsm(name: string): Promise ``` -------------------------------- ### loadEsm Function Source: https://github.com/borewit/load-esm/blob/master/README.md The core function of the load-esm library. It takes a package name or file path and returns a Promise that resolves to the imported module. ```APIDOC ## loadEsm ### Description Dynamically imports a pure ESM package or file path from a CommonJS (CJS) TypeScript project. ### Method `loadEsm(name: string): Promise` ### Parameters #### Path Parameters - **name** (string) - Required - Package name or file path to import. ### Returns - **Promise** - A Promise that resolves to the imported module namespace. ``` -------------------------------- ### Typed Dynamic ESM Import Source: https://github.com/borewit/load-esm/blob/master/README.md Import an ESM module with full TypeScript typings by providing a generic type argument to loadEsm. This ensures type safety when using the imported module. ```typescript import { loadEsm } from "load-esm"; (async () => { const esmModule = await loadEsm("esm-module"); // esmModule is fully typed })(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.