I’m having trouble with my Discord bot. It works fine when I start it using node bot.js
on my local machine and Linux VM. The SQLite database is read without any problems.
But when I try to use PM2 to run the bot in the background on my VM, it can’t seem to read the database. The m!shake
command doesn’t show the expected message.
I’ve tried looking into it, but I’m stumped. Does anyone know why PM2 might be causing issues with database access? How can I fix this so my bot works properly in the background?
Any help would be really appreciated. I’m not sure if it’s a path issue or something else entirely. Thanks in advance!
This sounds like a classic case of path resolution issues when running with PM2. When you start your bot directly with Node, it uses the current working directory. However, PM2 might be using a different working directory, causing the database file path to be incorrect.
Try using absolute paths for your database file instead of relative ones. You can use __dirname in your Node.js script to get the directory of the current file, then construct the full path to your SQLite database.
For example:
const path = require('path');
const dbPath = path.join(__dirname, 'your_database.sqlite');
Also, make sure PM2 has the necessary permissions to read and write to the directory where your database is stored. You might want to check the PM2 logs for any specific error messages related to file access.
If these steps don’t resolve the issue, you could try explicitly setting the working directory in your PM2 configuration file or start command.
hey dancingbird, sounds like a real pain! have u tried using absolute paths for ur database? PM2 can be finicky with working directories. also, double-check ur PM2 config file - might need to set the working dir explicitly there. good luck fixing it!
I ran into a similar issue when deploying my Discord bot with PM2. What worked for me was using the ‘cwd’ option in the PM2 ecosystem file to set the correct working directory. Here’s what I did:
- Create an ecosystem.config.js file in your project root.
- Add this configuration:
module.exports = {
apps: [{
name: 'my-discord-bot',
script: 'bot.js',
cwd: '/path/to/your/bot/directory'
}]
};
- Start your bot with ‘pm2 start ecosystem.config.js’
This ensures PM2 uses the right working directory, solving path-related issues. Also, make sure your database file has the correct permissions. Hope this helps!