Skip to content

Commit

Permalink
Add module-sync to ease Node v22 import
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Feb 8, 2025
1 parent addca23 commit 1588ada
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,17 +771,30 @@ Dependency list:

## CommonJS backward compatibility

For legacy CommonJS projects needing to load the `music-metadata` ESM module, you can use the `loadMusicMetadata` function:
Using Node.js ≥ 22, which is support loading ESM module via require
```js
const { loadMusicMetadata } = require('music-metadata');
const mm = require('music-metadata');
```

For older Node.js version < 22, you need to dynamically import **music-metadata**:
```js
(async () => {
// Dynamically loads the ESM module in a CommonJS project
const mm = await loadMusicMetadata();

const metadata = await mm.parseFile('/path/to/your/file');
const mm = await import('music-metadata');
})();
```

For CommonJS TypeScript projects, using a Node.js version < 22, you can use [load-esm](https://github.com/Borewit/load-esm):

This method shall replace the embedded CJS loader `loadMusicMetadata()` function.

```js
import {loadEsm} from 'load-esm';

(async () => {
// Dynamically loads the ESM module in a CommonJS project
const mm = await loadEsm<typeof import('music-metadata')>('music-metadata');
})();
```

> [!NOTE]
Expand Down

0 comments on commit 1588ada

Please sign in to comment.