Home > Software design >  Discord.js can't launch bot with process.env on the server with forever, but it works on my loc
Discord.js can't launch bot with process.env on the server with forever, but it works on my loc

Time:09-22

I have problem launching bot on the server with forever... I'm using process.env where I store BOT Token and Instagram login credentials. However on the server i cannot made bot work due this error..

(node:30365) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
ig-bot/node_modules/discord.js/src/client/Client.js:228
    if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
                                                   ^

Error [TOKEN_INVALID]: An invalid token was provided.
    at Client.login (ig-bot/node_modules/discord.js/src/client/Client.js:228:52)
    at Object.<anonymous> (ig-bot/ig.js:64:8)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  [Symbol(code)]: 'TOKEN_INVALID'
}
error: Forever detected script exited with code: 1
error: Script restart attempt #1
ig-bot/node_modules/discord.js/src/client/Client.js:228
    if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
                                                   ^

Error [TOKEN_INVALID]: An invalid token was provided.
    at Client.login (ig-bot/node_modules/discord.js/src/client/Client.js:228:52)
    at Object.<anonymous> (ig-bot/ig.js:64:8)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  [Symbol(code)]: 'TOKEN_INVALID'
}
error: Forever detected script exited with code: 1

It says token is invalid, but it really does not make any sense because it works on my PC and i can run the code via visual studio without problem. Same folder of the bot has trouble running on my server (usually i use config.json for token, but this time i need .env file)

//Process ENV code

const dotenv = require('dotenv');
dotenv.config({ path: './process.env' });
const username = process.env.IGUSERNAME
const password = process.env.IGPASSWORD
const TOKEN = process.env.TOKEN

client.login(TOKEN);

I tried ENV=production forever start yourApp.js forever -c "node -r dotenv/config" --workingDir app-workdir-path start app.js but none of these worked for me...

CodePudding user response:

After several hours I was able to find a solution.

const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '.env') })

This code will force to run your .env file from your script directory. console.log(__dirname) will return your path to the .js file. It solved the whole problem.

//Full code example

const path = require('path')
require('dotenv').config({ path: path.resolve(__dirname, '.env') })
const TOKEN = process.env.TOKEN
 
const { Client, Intents } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.on('ready', () => {
    client.user.setActivity("WE DID IT!", { type: 'PLAYING' });
    client.guilds.cache.forEach(guild => {
    let channel = guild.channels.cache.get('channelID')
    channel.send(`I'm finally ready!`);
    console.log('Wohoo! Bot is online!')
    })
})

// Login to Discord with your client's token
client.login(TOKEN);

CodePudding user response:

Based off of this answer I believe you should do sudo IS_PROD=1 forever start yourapp.js

Node.js forever with environment variable

  • Related