Skip to content

Commit 5867cf4

Browse files
committed
Add module-sync to ease Node v22 import
1 parent 604b421 commit 5867cf4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -771,17 +771,30 @@ Dependency list:
771771

772772
## CommonJS backward compatibility
773773

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

779+
For older Node.js version < 22, you need to dynamically import **music-metadata**:
780+
```js
778781
(async () => {
779782
// Dynamically loads the ESM module in a CommonJS project
780-
const mm = await loadMusicMetadata();
781-
782-
const metadata = await mm.parseFile('/path/to/your/file');
783+
const mm = await import('music-metadata');
783784
})();
785+
```
786+
787+
For CommonJS TypeScript projects, using a Node.js version < 22, you can use [load-esm](https://github.com/Borewit/load-esm):
788+
789+
This method replaced the CJS loader `loadMusicMetadata()` function.
784790

791+
```js
792+
import {loadEsm} from 'load-esm';
793+
794+
(async () => {
795+
// Dynamically loads the ESM module in a CommonJS project
796+
const mm = await loadEsm<typeof import('music-metadata')>('music-metadata');
797+
})();
785798
```
786799

787800
> [!NOTE]

0 commit comments

Comments
 (0)