ts-npm-ytmusic-api
ts-npm-ytmusic-api copied to clipboard
[BUG]: YTMusic is not a constructor
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)
How has this issue not surfaced earlier? Did it break with the newer versions of TypeScript?
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
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}`);
}
}
}
Still not working, what is the essence of keeping this project alive when nothing works and the author could not fix the bug???
anyone here know a very good alternative pkg??
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.
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...
I managed to reproduce the issue using codesandbox : https://codesandbox.io/p/devbox/agitated-sun-s2572k?file=%2Findex.mjs%3A4%2C38
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
apologies it was set as private by default. Should have access now.
nope still no access
TypeError: YTMusic is not a constructor
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.
Version v5.2.2 has been released with this bug fixed. Apologies for the very late fix and reply