• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

219
Vistas
Config.js file cannot import variables

I am making a large config.js file for all my messages (Discord.js V12). It works fine and everything, until the message I am trying to send contains variables (I am just stepping over from the situation with all variables spread, to one file). Somehow, the command file imports the variables from config.js, but not the other way around. This leaves me in a very tricky spot, where the config.js file gets rendered pretty much useless (for my situation). I switched from an .ENV file to a .JS file, specifically so that (I thought) I can include variables in my external file. Here is an sample for a problem:

Old situation:

const randomnumber = 5;

message.channel.send(`Hello world! ${randomnumber}`);

New situation, which doesn't work at the moment:

const config = require("./../../Other/config.js");
const randomnumber = 5;

message.channel.send(config.messages.testmessage);

Here is a piece of config.js (the randomnumber variable doesn't work, which is my problem):

exports.messages = { 
  testmessage: `Hello world! ${randomnumber}`),
}

I think that it doesn't work if also place const randomnumber = 5 in the config.js file, since somethimes the value after const, also contains variables of previous constructed variables. Or maybe, it is possible, but I am not sure. So that's my question.

almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The randomnumber variable isn't shared between files, unless it is explicitly imported in the config.js. In order to use that variable, it either needs to be declared in the config.js:

const randomnumber = 100;

exports.messages = {
  testmessage: `hello world: ${randomnumber}`
}

or, it can be shared using node imports and exports:

exports.randomnumber = 100

config.js:

const { randomnumber } = require('./otherfile.js');

exports.messages = {
  testmessage: `hello world: ${randomnumber}`
}
almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda