music-metadata: Can not import the library

Bug description I can not import the package from node. I spent 10h.

Expected behavior The package is imported and I can use the parseFile function.

Audio file demonstrating the problem ~/music-metadata
❯ npm i music-metadata

added 17 packages, and audited 18 packages in 1s

8 packages are looking for funding run npm fund for details

found 0 vulnerabilities

~/music-metadata-test  
❯ node
Welcome to Node.js v18.4.0.
Type ".help" for more information.
> require('music-metadata')
Uncaught:
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/janwirth/music-metadata/node_modules/music-metadata/lib/index.js not supported.
Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
    at __node_internal_captureLargerStackTrace (node:internal/errors:477:5)
    at new NodeError (node:internal/errors:388:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1139:19)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at require (node:internal/modules/cjs/helpers:102:18) {
  code: 'ERR_REQUIRE_ESM'
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 36 (14 by maintainers)

Most upvoted comments

@julianpoemp With version v10.3.1 you can update your CommonJS TypeScript code to:

import { loadMusicMetadata } from 'music-metadata';
import { createReadStream } from 'node:fs';

async function main() {
  try {
    // Create a readable stream from a file
    const audioStream = createReadStream('src/assets/EinsZweiDrei.wav');

    // Dynamic load the music-metadata ESM module
    const { parseStream } = await loadMusicMetadata();

    // Parse the metadata from the stream
    const metadata = await parseStream(audioStream, { mimeType: 'audio/wav' });

    // Log the parsed metadata
    console.log(metadata);
  } catch (error) {
    console.error('Error parsing metadata:', error.message);
  }
}

main();

Let’s see how generous the CommonJS community is: https://github.com/sponsors/Borewit

Downgrading to version 7 solved it for me.

@Borewit In the case the documentation contains the information the problem with the reader, in the case the documentation doesn’t contain a necessary information or it’s not clear, it’s the documentation 😃

Friendly recommendation: While you are arguing with me and trying to prove something, you could update your doc and close this issue with an answer: I’ve updated the doc accordingly so this is not a question anymore.

LOL, chill buddy. The doc didn’t mention that 7 and 8 are the same just different module systems, the doc says starting from v8 you drop CJS compatibility. Update your docs, answer less stupid questions after 😃

The lack of backward compatibility in ESM modules is a true nightmare @janwirth.

Can you please clone the test-repo I created earlier and show me, how to get 10.3 working? Neither the standard import nor your “CJS Backward compatibility” code works. With this example project you can reproduce the problem.

I think the problem you run into, is that there are no Typings defined (as mention on the README) for the loadMusicMetadata function.

To workaround the TypeScript type, try:

import mmCjs from 'music-metadata';
import { createReadStream } from 'node:fs';

type MusicMetadataCjs = {
  loadMusicMetadata: () => Promise<typeof import('music-metadata')>;
};

async function main() {
  try {
    // Create a readable stream from a file
    const audioStream = createReadStream('src/assets/EinsZweiDrei.wav');

    // Load the music-metadata ESM module
    const mmEsm = await (mmCjs as any as MusicMetadataCjs).loadMusicMetadata();

    // Parse the metadata from the stream
    const metadata = await mmEsm.parseStream(audioStream, { mimeType: 'audio/wav' });

    // Log the parsed metadata
    console.log(metadata);
  } catch (error) {
    console.error('Error parsing metadata:', error.message);
  }
}

main();

Please let me know the result.

Gents, unfortunately I do not have the time to try and test every framework.

I do not mind you to create or contribute to issues here to solve integration challenges, and share recommendations, but in my opinion the issues you describe are not caused by music-metadata.

I offer both CJS (in version 7) and ESM support (in version 8) module at the moment, a time consuming compensation from my end for the poor backward compatibility and lack of ESM clearly taking over. Pick the one you need.

In the ESM module, parseFile() is only exported to Node, as it depends on Node specific API’s. Client (browser) side typically follows the non-node path:

That distinction is made here, and is my opinion perfectly valid: https://github.com/Borewit/music-metadata/blob/ab248fb040cff30401c12767b3819c17b71e6115/package.json#L14-L19

Regarding using music-metadata in Electron, after having almost the same discussion, see this post

For now I’ll downgrade to 7.13 , the Vite CJS -> ESM seems to be working for that version.

music-metadata 7.13 is CommonJS, not ESM as already discussed

Electron ES Module support is still an open issue at this point in time.

@Borewit Just faced the same issue, are you sure the ESM exports are matching the CJS exports from v7? It seems like parseFile is not exposed.

"parseFile" is not exported by "node_modules/music-metadata/lib/core.js", imported by "src/main/modules/ipc-library.ts".

content of import * as mmd:

[Object: null prototype] [Module] {
  parseStream: [Function: parseStream],
  parseBuffer: [AsyncFunction: parseBuffer],
  parseFromTokenizer: [Function: parseFromTokenizer],
  orderTags: [Function: orderTags],
  ratingToStars: [Function: ratingToStars],
  selectCover: [Function: selectCover],
  scanAppendingHeaders: [AsyncFunction: scanAppendingHeaders]
}

I am using Vite, not sure if it is related.