Back to Blog

How to Run a Node.js Bot on a VPS

How to Run a Node.js Bot on a VPS
Command of the Day
node index.js

What it does: Executes the main Node.js JavaScript application file within the V8 engine runtime environment.

Node.js is the most popular runtime for building modern event-driven bots. With asynchronous non-blocking I/O and libraries like discord.js or telegraf, Node.js handles thousands of simultaneous WebSocket events with minimal memory overhead.

In this guide, we walk through deploying and running a Node.js bot on your RivinCloud VPS.


1. Step 1: Clone Your Repository or Upload Files

Connect to your VPS over SSH and clone your bot source code:

cd ~
git clone https://github.com/yourusername/my-bot.git
cd my-bot

Or upload your project folder using SFTP.


2. Step 2: Install Required NPM Dependencies

Run npm install inside your project directory to fetch all required node modules defined in package.json:

npm install --production

3. Step 3: Secure Configuration with .env

Create a .env file to store private credentials securely:

nano .env

Add your bot variables:

BOT_TOKEN=your_private_api_token_here
DATABASE_URL=mongodb://localhost:27017/mybot
PREFIX=!

In your Node.js code, access these variables using dotenv:

require('dotenv').config();
const token = process.env.BOT_TOKEN;

Command of the Day: node

The node executable is the CLI entrypoint for the Node.js JavaScript runtime.

Syntax

node [options] [script.js] [arguments]

Practical Examples

# 1. Direct execution of your bot entry script
node index.js

# 2. Check installed Node.js version
node -v

# 3. Enter interactive REPL environment to test quick JS logic
node

Explanation

When you run node index.js, V8 compiles your JavaScript into machine code and initializes the libuv event loop to process network events asynchronously.


4. Background Execution & Supervisor

Never rely on leaving your terminal open with node index.js. To keep your Node.js bot active after disconnecting from SSH, wrap execution inside PM2:

# Install PM2 globally
sudo npm install -g pm2

# Launch bot under PM2 supervisor
pm2 start index.js --name "nodejs-bot"

Common Mistakes

  • Uploading node_modules/ folder via SFTP: Never upload node_modules/ from your local Windows/Mac PC. Native binary bindings compiled for Windows will fail on Linux. Always run npm install on the Linux VPS directly!
  • Not setting .gitignore: Accidentally committing .env or node_modules/ to public Git repositories.

Troubleshooting

  • MODULE_NOT_FOUND Error: You forgot to run npm install or misspelled a required package inside package.json.
  • SyntaxError: Unexpected token: You are running an older Node.js version (e.g. Node 12 instead of Node 20). Upgrade Node.js via NodeSource setup script.

Best Practices

  • Use npm ci instead of npm install in production environments for deterministic builds matching package-lock.json.
  • Restrict file permissions on .env: chmod 600 .env.

Frequently Asked Questions

How do I restart my Node.js bot after updating code?

Pull your latest changes with git pull, run npm install if new packages were added, then restart PM2: pm2 restart nodejs-bot.


Conclusion

Running a Node.js bot on your RivinCloud VPS provides unmatched speed, flexibility, and reliability for all your automation needs.