zS1L3NT / ts-npm-ytmusic-api

A type-safe and basic YouTube Music API wrapper
https://npmjs.com/package/ytmusic-api
GNU General Public License v3.0
81 stars 17 forks source link

[BUG]: YTMusic is not a constructor #38

Closed LewdHuTao closed 3 months ago

LewdHuTao commented 3 months ago

Please describe the current behaviour of the code.

So previously i use the suggested fix in #30 to fix this issue by using const ytm = new YTMusic.default(); but after i update to v5.2.2 the error still occured but only on windows. When i tested on linux it works perfectly fine.

Please describe the expected behaviour of the code.

It should no longer return YTMusic is not a constructor

What are the steps to reproduce the error?

const YTMusic = require("ytmusic-api");

class Youtube {
  async getLyrics(title = null) {
    try {
      const ytm = new YTMusic();
      await ytm.initialize();

      const song = await ytm.searchSongs(title);
      const data = song[0];
      const artist_name = data.artist.name;
      const track_name = data.name;
      const search_engine = "YouTube";
      const artwork_url = data.thumbnails[1].url;
      const video_id = data.videoId;
      const lyrics_array = await ytm.getLyrics(video_id);
      const lyrics = lyrics_array.join("\n");

      return { artist_name, track_name, search_engine, artwork_url, lyrics };
    } catch {
      return { message: "No lyrics were found.", response: "404 Not Found" };
    }
  }
}

module.exports = Youtube;

Additional Information

This code is in javascript Tested on windows and linux ytmusic-api: v5.2.2 node.js: v20.10.0

LewdHuTao commented 3 months ago

I don't know if this is os related issue or not but right now I use this as temporarily fixed to make it work on both windows and linux

let ytm;

    try {
     // For windows
      ytm = new YTMusic.default();
      await ytm.initialize();
    } catch {
      // For linux
      ytm = new YTMusic();
      await ytm.initialize();
    }
LewdHuTao commented 3 months ago

Deleting node_modules and reinstall all package seems to fix the issue.