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)
@julianpoemp With version v10.3.1 you can update your CommonJS TypeScript code to:
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.
I think the problem you run into, is that there are no Typings defined (as mention on the README) for the
loadMusicMetadatafunction.To workaround the TypeScript type, try:
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
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
parseFileis not exposed.content of
import * as mmd:I am using Vite, not sure if it is related.