Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add require-esm compatibility #1033

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,27 +473,35 @@ async function getCoverArt(releaseGroupMbid, coverType = '') {

## CommonJS backward compatibility

For legacy CommonJS projects needing to load the `music-metadata` ESM module, you can use the `loadMusicMetadata` function:
I recommend CommonJS projects to consider upgrading their project to ECMAScript Module (ESM).
Please continue reading how to use **musicbrainz-api** in a CommonJS project.

Using Node.js ≥ 22, which is support loading ESM module via require, you can use:
```js
const { loadMusicBrainzApi } = require('musicbrainz-api');
const { MusicBrainzApi } = require('musicbrainz-api');
```

(async () => {
Other CommonJS projects have to use [dynamic import](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import) to load the **musicbrainz-api** pure ESM module:
```js
async function run() {
// Dynamically loads the ESM module in a CommonJS project
const { MusicBrainzApi } = await import('musicbrainz-api');
};

// Dynamically loads the ESM module in a CommonJS project
const {MusicBrainzApi} = await loadMusicBrainzApi();
run();
```

const mbApi = new MusicBrainzApi({
appName: 'my-app',
appVersion: '0.1.0',
appContactInfo: 'user@mail.org',
});
This is known not to work in TypeScript CommonJS projects, as the TypeScript compiler, in my opinion,
incorrectly converts the [dynamic import](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import)
to `require()`. To perform the dynamic import in TypeScript, you can use [load-esm](https://github.com/Borewit/load-esm):

const releaseList = await mbApi.search('release', {query: {barcode: 602537479870}});
for(const release of releaseList.releases) {
console.log(release.title);
}
})();
```
```js
import {loadEsm} from 'load-esm';

> [!NOTE]
> The `loadMusicMetadata` function is experimental.
async function run() {
// Dynamically loads the ESM module in a TypeScript CommonJS project
const { MusicBrainzApi } = await loadEsm<typeof import('musicbrainz-api')>('musicbrainz-api');
};

run();
```
5 changes: 0 additions & 5 deletions lib/entry-default.cjs

This file was deleted.

5 changes: 0 additions & 5 deletions lib/entry-default.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
export * from './coverartarchive-api.js';
export * from './musicbrainz-api.js';

/**
* CommonJS (only) function to load `musicbrainz-api` ESM module
*/
export declare function loadMusicBrainzApi(): Promise<typeof import('musicbrainz-api')>;
5 changes: 0 additions & 5 deletions lib/entry-node.cjs

This file was deleted.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
"description": "MusicBrainz API client for reading and submitting metadata",
"exports": {
"node": {
"import": "./lib/entry-node.js",
"require": "./lib/entry-node.cjs"
"types": "./lib/entry-node.d.ts",
"default": "./lib/entry-node.js"
},
"default": {
"import": "./lib/entry-default.js",
"require": "./lib/entry-default.cjs"
"types": "./lib/entry-default.d.ts",
"default": "./lib/entry-default.js"
}
},
"types": "lib/index.d.ts",
"types": "lib/entry-default.d.ts",
"files": [
"lib/**/*.js",
"lib/**/*.d.ts",
"lib/entry-node.cjs",
"lib/entry-default.cjs"
"lib/**/*.d.ts"
],
"type": "module",
"author": {
Expand Down
Loading