Skip to content

Commit 23cb13e

Browse files
committed
Initial commit
1 parent a118ab5 commit 23cb13e

5 files changed

Lines changed: 144 additions & 0 deletions

File tree

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Project
2+
node_modules/
3+
4+
#IntelliJ IDEA:
5+
.idea
6+
*.iml
7+
8+
# Yarn:
9+
.pnp.*
10+
.yarn
11+
yarn-error.log

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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)

index.cjs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
loadModule: (module) => import(module)
3+
};

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function loadModule<T = any>(name: string): Promise<T>;

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "load-esm",
3+
"version": "0.1.0",
4+
"main": "index.cjs",
5+
"types": "index.d.ts",
6+
"author": {
7+
"name": "Borewit",
8+
"url": "https://github.com/Borewit"
9+
},
10+
"funding": [
11+
{
12+
"type": "github",
13+
"url": "https://github.com/sponsors/Borewit"
14+
},
15+
{
16+
"type": "buymeacoffee",
17+
"url": "https://buymeacoffee.com/borewit"
18+
}
19+
],
20+
"license": "MIT",
21+
"description": "Utility to dynamically load ESM modules in TypeScript CommonJS projects",
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/Borewit/load-esm.git"
25+
},
26+
"bugs": {
27+
"url": "https://github.com/Borewit/load-esm/issues"
28+
},
29+
"homepage": "https://github.com/Borewit/load-esm#readme",
30+
"keywords": [
31+
"load-esm",
32+
"ESM",
33+
"CommonJS",
34+
"TypeScript",
35+
"Node.js",
36+
"dynamic import",
37+
"JavaScript modules",
38+
"module interoperability",
39+
"mixed-module environment"
40+
],
41+
"engines": {
42+
"node": ">=13.2.0"
43+
},
44+
"dependencies": {}
45+
}

0 commit comments

Comments
 (0)