Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
How do you store JavaScript variables in a different file?

I have a Discord bot project with lots of variables (numbers, strings etc). I now use .ENV to store my variables. I would like to change that. I learned that you can use .ENV for sensitive variables. For example (Discord.js), your bot token. I have tried searching online, but couldn't find the right answer. Ideally, I would like to have just one file with all my strings. For example:

botcolor = #ffffff

New:

const botcolor = ./config.js

botcolor = ${botcolor}

A different example with a string (when you do ?ping)

message.channel.send("Pong!")

New:

const answer = ./config.js

message.channel.send(`${answer}`)

Something like that. I'm not very experienced with JS. So I'm sorry if im not very clear. Since I have multiple bots, I want to have one config file. My bot is like a template, I can add different types of configs, so that the whole bot (color, answers) changes. My bot is on Discord.js v12 btw, since updating would mean almost rewritting my bots.

So does anyone have a solution for an external config file?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you're using Node.JS, you can use CommonJS requires. I imagine at the top of your main JavaScript file you have a snippet that goes something like:

const discord = require("discord.js");

You can do that for your own files too!
Consider this basic file setup:

- config.js
- index.js <-- entry file

Inside config.js, you export data for another file to use. We can do it like this:

module.exports = {
  answer: "blah blah blah",
  botColor: "#fff",
};

You can export anything, but the most common is either a function or object (which we just did). Now we can use the config file like this:

// index.js
const config = require("./config");

It's important to add ./ in front of your file or Node will try to import from /node_modules. Then after you imported it your could just use it as it were an object:

message.channel.send(config.answer); // <-- "blah blah blah"

You can export multiple objects:

// config.js
exports.bot = {
  answer: "blah blah blah"
};
exports.colors = {
  main: "#fff"
};

Then you use it like:

// index.js
const config = require("./config");
const answer = config.bot.answer;

Or with destructuring:

// index.js
const { bot, colors } = require("./config");
bot.answer; // <-- "blah blah blah"
colors.main // <-- "#fff"

Note: You can also do require("./config.js"), but I find the syntax without .js simpler to understand. You can also require json files as objects.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda