Search code examples
githubherokudiscord.js

process.env.token not working on discord bot startup


I am trying to make my discord bot stay online 24/7 using Heroku and Github. but whenever I try to run my code I get this error.

2022-01-01T03:56:43.950025+00:00 app[Worker.1]: /app/node_modules/discord.js/src/rest/APIRequest.js:33
2022-01-01T03:56:43.950043+00:00 app[Worker.1]:     agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
2022-01-01T03:56:43.950043+00:00 app[Worker.1]:           ^^^
2022-01-01T03:56:43.950044+00:00 app[Worker.1]: 
2022-01-01T03:56:43.950044+00:00 app[Worker.1]: SyntaxError: Unexpected token '??='

There is two spots where I put my token, and I use process.env.token in those slots. First one is my index.js file

(async () => {
    for (file of functions) {
        require(`./src/functions/${file}`)(client);
    }
    client.handleEvents(eventFiles, ".src/events");
    client.handleCommands(commandFolders, "./src/commands");
    client.login(process.env.token);
})();

second is my handlecommands.js file

module.exports = (client) => {
    client.handleCommands = async(commandFolders, path) => {
        client.commandArray = [];
        for (folder of commandFolders) {
            const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));

            for (const file of commandFiles) {
                const command = require(`../commands/${folder}/${file}`);
                // Set a new item in the Collection
                // With the key as the command name and the value as the exported module
                client.commands.set(command.data.name, command);
                client.commandArray.push(command.data.toJSON());
            }
        }
        const rest = new REST({
             version: '9' 
        }).setToken(process.env.token);

How can I fix this? (Quick Disclaimer, I do not have a .env file but I do have a procfile telling Heroku what to do) Worker: node index.js


Solution

  • Seems like your Heroku App is using a outdated / older version of Node.js, where ??= isn't available. Since you can't directly update the Node.js version for your Heroku app, you can set the version you need in your package.json.

    Simply add the following lines after the, e.g. "description", object:

    "engines": {
        "node": "16.x" (or another version, depending on which one you need)
    }