ts-npm-ytmusic-api icon indicating copy to clipboard operation
ts-npm-ytmusic-api copied to clipboard

[BUG]: YTMusic is not a constructor

Open SomeAspy opened this issue 11 months ago • 11 comments

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)

SomeAspy avatar Mar 18 '24 03:03 SomeAspy

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

zS1L3NT avatar Mar 18 '24 03:03 zS1L3NT

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

SomeAspy avatar Mar 18 '24 04:03 SomeAspy

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}`);
    }
  }
}

tomwallyn avatar Mar 25 '24 09:03 tomwallyn

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

omathdevelop avatar Apr 09 '24 23:04 omathdevelop

anyone here know a very good alternative pkg??

omathdevelop avatar Apr 09 '24 23:04 omathdevelop

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.

SomeAspy avatar Apr 10 '24 21:04 SomeAspy

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...

zS1L3NT avatar Apr 11 '24 04:04 zS1L3NT

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

raed667 avatar Apr 11 '24 07:04 raed667

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

SomeAspy avatar Apr 11 '24 23:04 SomeAspy

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

raed667 avatar Apr 12 '24 07:04 raed667

nope still no access

TypeError: YTMusic is not a constructor

femke77 avatar Apr 13 '24 00:04 femke77

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.

Dajust avatar May 22 '24 03:05 Dajust

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

zS1L3NT avatar Jul 07 '24 21:07 zS1L3NT