0

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

RasmonT
  • 391
  • 2
  • 13

2 Answers2

2

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);
RasmonT
  • 391
  • 2
  • 13
0

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

Node.js forever with environment variable

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Same problem... this also is not working – RasmonT Sep 17 '21 at 20:17
  • honestly, i use PM2 (which is similar to forever) with a .env file in my root dir and i havent had any issues... when i google around people just say to run it like you would run regular node with environment variables – Garett Friesen Sep 17 '21 at 20:29
  • Not sure what is PM2, I'm fine with forever but i can't find way to make it work... NODE_ENV=production forever ig-bot/ig.js is also not working... – RasmonT Sep 17 '21 at 20:31
  • PM2 is a process manager that lets you run things indefinitely. try passing your environment variables through the command line like `ENV=production forever start yourApp.js`, but it is possible forever just does not like dotenv or environment variables at all – Garett Friesen Sep 17 '21 at 20:39
  • Ugh this is really weird... i tried simple node ig-bot/ig.js and it says same error ... invalid token.. but it literally does not make sense, it's literraly same copy that is working on my desktop via Visual Studio Code... I don't understand what the hell is going on. – RasmonT Sep 17 '21 at 20:42
  • try changing your dotenv config code to `dotenv.config({ path: '..env' });` on your server and make sure your env file is in the root directory (same as ig.js) and rename it to just `.env`. dotenv expects the file to be a hidden file with no name just .env – Garett Friesen Sep 17 '21 at 20:48
  • Same... this is really weird glitch. I even tried simple require('dotenv').config() and renamed env file to just `.env` and still the same issue... – RasmonT Sep 17 '21 at 20:57
  • yeah, very weird... i would recommend trying pm2 as an alternative, it is an npm package that should let you do what you need to: https://pm2.keymetrics.io/docs/usage/quick-start/ – Garett Friesen Sep 17 '21 at 21:01
  • I tried also.. didn't worked... even simple node ig-bot/ig.js is saying same shit... it can't read the token... This is maybe some .js glitch but how to fix it? – RasmonT Sep 17 '21 at 21:02