aadsm / jsmediatags

Media Tags Reader (ID3, MP4, FLAC)
Other
745 stars 128 forks source link

browser version in node environment creating problem #104

Closed FritzHeiden closed 5 years ago

FritzHeiden commented 5 years ago

When trying to use the browser version (dist/jsmediatags.js) in a node.js environment it is impossible to use the library.

if (typeof process !== "undefined" && !process.browser) {
  Config.addFileReader(NodeFileReader);
}

It does some initialization, where adding the NodeFileReader doesn't seem to work as it just adds an empty object ({}) which in turn causes the findFileReader() method to fail when trying to read the tags.

for (var i = 0; i < mediaFileReaders.length; i++) {
  if (mediaFileReaders[i].canReadFile(this._file)) {
    return mediaFileReaders[i];
  }
}

Obviously this method call doesn't work because at some point mediaFileReaders[i] will be {}.

I hope I could make sense of the issue I have. For now I just use a copy of the script, where I changed the findFileReader() method to

for (var i = 0; i < mediaFileReaders.length; i++) {
  if (!mediaFileReaders[i] || !mediaFileReaders[i].canReadFile) continue;
  if (mediaFileReaders[i].canReadFile(this._file)) {
    return mediaFileReaders[i];
  }
}
aadsm commented 5 years ago

Yeah, in the browser version we don’t have it because that reader depends on the filesystem module. Why not use the npm package?

FritzHeiden commented 5 years ago

I'm writing a library which I use in different environments: node, react native and the browser.

aadsm commented 5 years ago

Oh, I see. Yeah, that’s more tricky as the browser version is a compilation of the original code which is targetting node. You can see that here: https://github.com/aadsm/jsmediatags/blob/master/package.json#L72. My recommendation is to use the npm version of this package and do something similar in your build process.