How to run a Discord bot in Node.js on hosting
Run a Node.js Discord bot (discord.js) on a Hostero server: the bot's files, the token in .env, libraries from package.json and TypeScript files.
A Node.js bot server installs the libraries from package.json and runs the bot's start file on every start. The example uses discord.js (opens in a new tab), but you run a bot in another library, such as Eris or Oceanic.js, the same way.
The client panel is available in Polish only, so this article gives its labels in Polish with a translation the first time they appear.
Before you start
You need a bot created in the Discord Developer Portal and its token. How to create a Discord bot shows how to get them.
The bot's files
In the Pliki (Files) tab, create three files: click Nowy plik (New file), paste the content, click Stwórz plik (Create file) and enter the name. You can upload files from your computer with Wgraj plik (Upload file) or over SFTP. Don't upload the node_modules folder, the server installs the libraries itself.
The token in a .env file
Keep the token in a .env file, not in the code. That way it doesn't leak when you show someone the code or push it to GitHub.
TOKEN=<your-token>The bot's code
An index.js file with a bot that logs in to Discord and answers the /ping command. process.loadEnvFile() loads the token from the .env file with no extra libraries.
process.loadEnvFile();
const { Client, Events, GatewayIntentBits, SlashCommandBuilder } = require("discord.js");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const ping = new SlashCommandBuilder()
.setName("ping")
.setDescription("Checks that the bot responds");
client.once(Events.ClientReady, async (readyClient) => {
await readyClient.application.commands.set([ping.toJSON()]);
console.log(`Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === "ping") {
await interaction.reply(`Pong! ${client.ws.ping} ms`);
}
});
client.login(process.env.TOKEN);The list of libraries
A package.json file with the libraries in the dependencies section:
{
"name": "my-bot",
"private": true,
"main": "index.js",
"dependencies": {
"discord.js": "^14.28.1"
}
}Starting the bot
Click Uruchom (Start) in the Konsola (Console) tab. The server installs the libraries with npm install and runs index.js. When you see Logged in as, the bot is online on Discord.
Server settings
In the Parametry startowe (Startup parameters) tab, you can change how the server runs the bot:
- Wersja środowiska (Obraz Docker) (Environment version): the Node.js version, from 12 to 26. discord.js 14 needs at least Node.js 18, and
process.loadEnvFile()at least 20.12. The change takes effect after the server restarts. - Plik startowy (Start file): the file the server runs.
index.jsby default. If your bot starts frombot.jsorsrc/index.js, enter that path here. - Dodatkowe biblioteki NodeJS (Extra Node.js libraries): libraries to install on start, separated by spaces, such as
dayjs mongoose. The server adds them topackage.json. - Odinstaluj biblioteki NodeJS (Uninstall Node.js libraries): libraries to remove on start, also separated by spaces.
- Dodatkowe argumenty (Extra arguments): arguments passed to the bot after the file name.
Libraries
The server installs the libraries from package.json. If you also upload the package-lock.json file, npm install installs exactly the versions you have locally, so the bot behaves the same after a restart.
You can add a new library in two ways:
- On your computer: run
npm install <name>in the bot's folder, then upload the newpackage.jsonandpackage-lock.json. - In the panel: enter the name in the Dodatkowe biblioteki NodeJS field and restart the server. After the install, you can clear the field, because the library is already in
package.json.
TypeScript
If you enter a .ts file, such as index.ts, in the Plik startowy field, the server runs it with ts-node, without compiling first. For bigger projects, it's better to compile on your computer with tsc and enter the compiled .js file as the start file, such as dist/index.js.
Common problems
Error: Cannot find module …
A library is missing. Add it to dependencies in package.json or to the Dodatkowe
biblioteki NodeJS field and restart the server. If it's your bot's own file that's missing,
check the Plik startowy field.
TypeError: process.loadEnvFile is not a function
The server has an older Node.js version. In Startup parameters, choose Node.js 22 or newer in the Wersja środowiska field and restart the server.
Error [TokenInvalid]: An invalid token was provided
The token is wrong or was reset. Generate a new one in the Developer Portal and enter it in
.env after TOKEN=, without quotes or spaces.
Used disallowed intents
The code asks for an intent that isn't turned on in the portal, such as MessageContent. Turn
it on in the Developer Portal, in the Bot tab, under Privileged Gateway Intents.
SyntaxError: Cannot use import statement outside a module
The code uses import, but the project isn't set up as an ES module. Add "type": "module" to
package.json or replace import with require.