Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Getting a TypError saying that my exported function is not a function with nodejs

My goal is to create a discord bot that plays Spotify music, but when I run the function that plays music it gives me the error:

TypeError: playSong is not a function

Here is what is in my api.js:

const playSong = (songName) => {
    spotifyApi.searchTracks(songName)
        .then(data => {
            const track = data.body.tracks.items[0];
            const trackId = track.id;
            spotifyApi.play({
                device_id: '',
                uris: [`spotify:track:${trackId}`]
            });
        })
        .catch(err => {
            console.error(err);

        });
};

module.exports = { playSong };

And here is in my play.js:

const { playSong } = require('../spotify_api/api.js');

let msg = message.content
        let song = msg.replace('!play ', '');

        //creates the audio player
        if (message.content === `!play ${song}`) {
            //VoiceChannel IDs

            //Audio Player
            const player = createAudioPlayer()
            player.on(AudioPlayerStatus.Playing, () => {
                client.message.reply(`Now playing: ${song}`)
                console.log(`Now playing: ${song}`)
            })

            player.on('error', err => {
                console.log(`Error: ${err.message} with resource`)
            })

            //creat and play audio
            player.play(playSong(song)); //This is where I call the function

            //Subscribe the connection to the audio player
            const subscription = connection.subscribe(player)

            if (subscription) {
                //Unsubscribe the subscription
                setTimeout(() => subscription.unsubscribe(), 120000)
            }


        }

I have tried to use other methods I know of but it all comes down to the same TypeError. T_T

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try to replace

const playSong = () => 

with

function playSong()
about 4 years ago · Juan Pablo Isaza Report

0

Instead of

module.exports = { playSong };

Use

module.exports.playSong = playSong;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!