|
| 1 | +# load-esm |
| 2 | + |
| 3 | +***load-esm*** is a utility for dynamically importing pure ESM (ECMAScript Module) packages in CommonJS TypeScript projects. |
| 4 | +This is particularly useful when working with Node.js environments configured for CommonJS while needing to integrate ESM-only dependencies. |
| 5 | + |
| 6 | +## Installation |
| 7 | +```bash |
| 8 | +npm install load-esm |
| 9 | +``` |
| 10 | + |
| 11 | +or |
| 12 | + |
| 13 | +```bash |
| 14 | +yarn add load-esm |
| 15 | +``` |
| 16 | + |
| 17 | +## Features |
| 18 | +- Enables dynamic import of pure ESM modules in CommonJS-based projects. |
| 19 | +- Compatible with TypeScript, offering proper type definitions. |
| 20 | +- Simple and lightweight implementation. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +Here’s an example demonstrating how to use load-esm to dynamically load an ESM module in a CommonJS project: |
| 25 | + |
| 26 | +```ts |
| 27 | +import * as path from 'path'; |
| 28 | +import { loadModule } from 'load-esm'; |
| 29 | + |
| 30 | +/** |
| 31 | + * Import 'file-type' ES-Module in CommonJS Node.js module |
| 32 | + */ |
| 33 | +(async () => { |
| 34 | + try { |
| 35 | + // Dynamically import the ESM module |
| 36 | + const { fileTypeFromFile } = await loadModule('file-type'); |
| 37 | + |
| 38 | + // Use the imported function |
| 39 | + const type = await fileTypeFromFile('fixture.gif'); |
| 40 | + console.log(type); |
| 41 | + } catch (error) { |
| 42 | + console.error('Error importing module:', error); |
| 43 | + } |
| 44 | +})(); |
| 45 | +``` |
| 46 | + |
| 47 | +## API |
| 48 | + |
| 49 | +```ts |
| 50 | +loadModule<T = any>(name: string): Promise<T> |
| 51 | +``` |
| 52 | +Dynamically imports an ESM module. |
| 53 | +
|
| 54 | +#### Parameters |
| 55 | +- `name` (string): The name or path of the module to load. |
| 56 | +
|
| 57 | +#### Returns |
| 58 | +- A `Promise<T>` that resolves to the imported module object. |
| 59 | + |
| 60 | +### Example |
| 61 | +```ts |
| 62 | +import { loadModule } from 'load-esm'; |
| 63 | + |
| 64 | +(async () => { |
| 65 | + const module = await loadModule('some-esm-module'); |
| 66 | + console.log(module); |
| 67 | +})(); |
| 68 | +``` |
| 69 | + |
| 70 | +## How It Works |
| 71 | +The utility leverages Node.js's import() function to dynamically load ESM modules. |
| 72 | +This allows CommonJS-based projects to interact with ESM dependencies without converting the entire project to ESM. |
| 73 | + |
| 74 | +## Compatibility |
| 75 | +- Node.js: Requires Node.js 13.2.0 or later, as import() is only supported in these versions and beyond. |
| 76 | +- TypeScript: Fully typed and compatible with TypeScript. |
| 77 | + |
| 78 | +## Why Use load-esm? |
| 79 | +In mixed-module environments where your project is primarily CommonJS but relies on some ESM-only dependencies, |
| 80 | +load-esm serves as a seamless bridge, |
| 81 | +enabling interoperability without changing your project’s module system. |
| 82 | + |
| 83 | +## License |
| 84 | +[MIT](./LICENSE) |
0 commit comments