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 #30

Closed SomeAspy closed 3 months ago

SomeAspy commented 7 months ago

Please describe the current behaviour of the code.

Following the example in the readme will cause a TypeError: YTMusic is not a constructor

Please describe the expected behaviour of the code.

This error should not occur

What are the steps to reproduce the error?

Slightly modified code based off the readme example:

import YTMusic from "ytmusic-api";

import untypedConfig from "../config/config.json" assert { type: "json" };
import type { Config } from "./types/config.js";
const { YouTubeMusicCookie } = untypedConfig as Config;

const YTM = new YTMusic(); // "TypeError: YTMusic is not a constructor" on build

await YTM.initialize({cookies:YouTubeMusicCookie})

await YTM.getArtist("Dirt Poor Robins").then(results=>{
    console.log(results)
});

Additional Information

typescript 5.4.2 (also tested with 5.2.2) tsx 4.7.1 ytmusic-api 5.2.1 node 21.7.1 (also tested with 18.19.1)

zS1L3NT commented 7 months ago

How has this issue not surfaced earlier? Did it break with the newer versions of TypeScript?

SomeAspy commented 7 months ago

I am unsure, this is my first time working with this package. I also found a near identical issue on another Youtube Music API package (https://github.com/nickp10/youtube-music-ts-api/issues/33) There is a possibility this could be on me if you are unable to reproduce. I can send my tsconfig.json too if it might help

tomwallyn commented 7 months ago

I had the same problem. You can use the .default method which resolved the issue for me.

import YTMusic from 'ytmusic-api'

export default class YtbMusicService {
  private ytmusic: any

  constructor() {
    this.ytmusic = new YTMusic.default()
  }

  async searchMusics(query: string) {
    try {
      await this.ytmusic.initialize()
      const searchResults = await this.ytmusic.search(query)
      return searchResults
    } catch (error) {
      throw new Error(`Failed to search for musics: ${error}`);
    }
  }
}
omathdevelop commented 6 months ago

Still not working, what is the essence of keeping this project alive when nothing works and the author could not fix the bug???

omathdevelop commented 6 months ago

anyone here know a very good alternative pkg??

SomeAspy commented 6 months ago

Still not working, what is the essence of keeping this project alive when nothing works and the author could not fix the bug???

While I understand the frustration behind finding a package you need to use seemingly unmaintained, we shouldn't berate the developer, as they are unpaid and do this in their free time.

zS1L3NT commented 6 months ago

Apologies for the late responses. Thanks @SomeAspy for the understanding.

I can't find a good way to reproduce this? I created a new node project and installed ytmusic-api into it and it worked fine...

raed667 commented 6 months ago

I managed to reproduce the issue using codesandbox : https://codesandbox.io/p/devbox/agitated-sun-s2572k?file=%2Findex.mjs%3A4%2C38

SomeAspy commented 6 months ago

I managed to reproduce the issue using codesandbox : codesandbox.io/p/devbox/agitated-sun-s2572k?file=%2Findex.mjs%3A4%2C38

This link appears to be a 404 unless you negotiated perms with the repo owner some other way image

raed667 commented 6 months ago

apologies it was set as private by default. Should have access now.

femke77 commented 6 months ago

nope still no access

TypeError: YTMusic is not a constructor

Dajust commented 5 months ago

Thanks, @tomwallyn, that actually works!

import YTMusic from 'ytmusic-api';

const ytm: YTMusic = new YTMusic.default();
await ytm.initialize();

I guess the problem is in how the package does exports. Something isn't quite right.

zS1L3NT commented 3 months ago

Version v5.2.2 has been released with this bug fixed. Apologies for the very late fix and reply